I have a form_for tag that I want to use the index action in the search_results controller.
My tag looks like the following.
<%=form_for(:search_results, url:{controller: :search_results, action: :index}, html:{id:"quickSearch"}) do | f| %>
...form fields...
<% end %>
However when I try to enter something into my form and submit it, it is going to the create action instead of the index action? Normally this would make sense if I didn't specify the action, but I did. I also tried removing the action and making the tag use the get method but it never hits the controller and I need it to do that.
Related
What is the best practice using hidden fields in html views?
In my html.erb I need to check if a value exists, if it does then u want to ensure a button remains disabled.
Can I set a hidden field in ruby code like this ;
<% if #is_draft %>
<input type="hidden" id="isdraft "value="true"><% end %>
Then can I access this in my jquery code?
Is this accepted practice? Note that the javascript is in its own file, the script is not in the html.erb file
It is regarded as pretty poor practice most of the time.
Instead it is usually better to render the button as disabled on the server side after checking the condition.
Also make sure to sanitize the requests made by this form (button is clicked although your condition does not permit it). Users could un-disable the button and click it.
If you find that you have to save some information in HTML for whatever reason, I would resort to data-*-attributes!
Hope that helps!
EDIT pseudocode example:
# MyView.html.erb
<% unless #is_draft %>
<%= render partial: 'my_checkbox_button_enabling', format: [:js] %>
<% end %>
<%= button_tag "My nice button", disabled: true, id: "my-button" %>
I'm having trouble routing a button so that it calls an action in a controller. To be more specific, I have this line
delete 'destroy/:id', to: 'users#admin_destroy'
in my route file, and this line
%= button_to 'Destroy', destroy_path(user), data: { confirm: 'Are you sure?' } %>
in a view.
The problem is that the page keeps treating destroy_path as a method, and I'm at a loss for what the proper syntax should be. What am I misunderstanding?
Additionally, I originally tried to get this work as link_to, but I learned that that should be reserved for GET requests. However, I don't like how button_to looks, and I'm wondering if there's a way to do this as a GET so that link_to can be used instead.
I'm not sure if this will completely solve your problem, but... You can custom define a path like so:
in config/routes.rb
delete 'destroy/:id', to: 'users#admin_destroy', as: :destroy
This will allow you to use destroy_path in your application, and have it route to your users controller with the admin_destroy function.
You can read more about it here
I'm having a routing issue with an image. In my app I have images of items on the home page. I would like them to link to their image page.
Here is what my items controller looks like:
class ItemsController < ApplicationController
def show
#item = Item.find(params[:id])
end
end
This is what I have in my routes:
Rails.application.routes.draw do
resources :items
end
And this is what I have in the item partial:
<%= link_to(image_tag(item.image.url(:thumb)), item_path(:id)) %>
What I expected after reading the rails routing guide was that this would link to the item page for that image. Here is their example:
photo_path(:id) returns /photos/:id (for instance, photo_path(10) returns /photos/10)
I should also add that this is in my home page controller:
class StaticPagesController < ApplicationController
def home
#items = Item.where.not(category_id: 1)
end
However, that is not working. I've tried several different things, but all produce errors. Is there a simple way to do this?
The normal way to do what you want is this:
<%= link_to item_path(item) do %>
<%= image_tag(item.image.url(:thumb)) %>
<% end %>
You can just pass the instance of the item to item_path and also if you have complicated html for a link, it is usual to put it in a block for the link as shown here (with link_to something do).
In my app I have Issue model, which belongs_to Status model. What I need to do is to have dropdown list of links (or spans, doesn't matter) with Statuses Id's on Issue show page, so I could change status clicking on this (could be non-ajax and ajax).
I am rather new to Rails, so I do not know how to implement this, was thinking of few ways to do this, but non seems to be working.
Because you're new, I'll outline what I'd do for you (hopefully it will help):
#config/routes.rb
resources :issues do
get "change_status/:status_id", to: "issues#change_status"
end
#app/models/status.rb
Class Status < ActiveRecord::Base
has_many :issues
scope :not, ->(id) { where('statuses.id != ?', id) }
end
#app/controllers/issues_controller.rb
def show
#issue = Issue.find params[:id]
#statuses = Status.all.not(#issue.id)
end
def change_status
issue = Issue.find params[:id]
issue.status = params[:status_id]
issue.save if issue.status_id_changed?
redirect_to issue
end
#app/views/issues/show.html.erb
<%= #statuses.each do |status| %>
<%= link_to status.title, issue_change_status_path(#issue, status) %>
<% end %>
There's obviously some stuff to explain - if you need me to do that, let me know and I'll give you the details!
You can simply use the link_to rails helper like in the following code:
link_to "Change Status", my_path_to_update_status, remote: true, method: :put
This will send an AJAX PUT request (thank to the remote: true) to your my_path_to_update_status.
Then, you just have to update the status in the action corresponding to the path specified in the link_to helper tag.
More information about the link_to rails helper here
Hi I want to preface this by saying that I am new to coding.
I have an issue that I believe can be solved in two ways
A. by rendering a partial
B. by updating the controller
( I can totally be wrong but these are what I suspect lol)
I have two controllers/views "reviews" and "logs". and I want them to both appear on the same page.
How can I do this?
I tried rendering a partial but I alway get an error.
and I tried the piece of code below:
which made my reviews show up on the page, but when I add
#log = #user.logs.all
to it, it doesn't pull all the logs to the page like it does for the reviews.
def show
#user = User.find_by_name(params[:id])
if #user
#reviews = #user.reviews.all
render action: :show
else
render file: 'public/404', status: 404, formats: [html]
end
end
First things first. Views refer to actions in controllers. So there can be several views for each controller or even none.
So, if you want to render #reviews and #logs on the same page you should first instantiate both instance variables in the same action and then render both partials in the same action.
How do you do that? Easy.
First you got to the controller you just showed and edit that show action.
def show
# You can set the variable in the if-clause
# And you also need to use static finders with a hash as an argument in Rails4
if (#user = User.find_by(name: params[:id]))
#reviews = #user.reviews.all
#logs = #user.logs.all
# You don't need to call render explicitly
# if you render the view with the same name as the action
else
render file: 'public/404', status: 404, formats: [html]
end
end
Second: you go to your /app/views/reviews/show.html.erb template and put both partials there like this (this is just an example, adjust your markup to fit your needs).
<h1> Reviews and Logs</h1>
<div id="reviews_part">
<%= render #reviews %>
</div>
<div id="logs_part">
<%= render #logs %>
</div>
Now create 2 new partials /app/views/reviews/_review.html.erb and /app/views/logs/_log.html.erb and put all the needed markup there (use regular variables review and log to adress the repeating objects). Rails will automaticaly repeat those partials as many times as needed.
Or you can explicitely call the partial render
<div id="reviews_part">
<% #reviews.each do |review| %>
<%= render review %>
which is the same as
<%= render partial:"reviews/review", locals:{review:review} %>
<% end %>
</div>
Here is the way of rendering partials into views in HAML:
=render :partial => "header"
%h2 Hello World
=render :partial => "footer"
Every partial you render this way, has to be created within the same folder.
Each partial's name has to begin with an underscore (_). This should be the view's directory:
- home
- index.html.haml
- _header.html.haml
- _footer.html.haml