This seems like it should be so simple but it has been causing me issues.
I have a select_tag that pulls from a model. All I want to is have a person choose their location from the drop down, press submit and take it to that places page.
Here is what I have
<% form_tag installation_path do %>
<%= select_tag :id, options_from_collection_for_select(Installation.find(:all), :id, :name) %>
<div id="button">
<p>
<%= submit_tag "Go", :name => nil %>
</p>
</div>
The problem is it of course wants an :id but it won't pull the :id from the drop down menu below.
What am I doing wrong, any other suggestions on the "right" way to do this.
Thanks
Looks like you actually want to GET, not to POST the params.
form_tag installation_path, :method => :get do
Related
I have a ruby on rails app that has a form. I was wondering if there is a way to make sure that a user has selected drop down menu items in both of the drop downs on this form before it is submitted and params are generated. Ideally I would like to throw and error warning them to pick 1 item on each of the drop downs and re-render the form.
The form is below:
<%= form_tag compare_products_path, method: :get do |f| %>
<%= select_tag :product_id1, options_from_collection_for_select(#products, 'id', 'name') %>
<%= select_tag :product_id2, options_from_collection_for_select(#products, 'id', 'name') %>
<%= f.submit %>
<% end %>
Please let me know how I can accomplish what I stated above.
SIDENOTE: I also implemented Select2 to make the form look nicer but could not find out of there is a quick validation trick in Select2 to accomplish what I said above, if there is a suggestion for that I can post the Select2 version,
Try this:
<%= select_tag :product_id1, options_from_collection_for_select(#products.where.not(:name=>nil), 'id', 'name'), :include_blank => "Please select...",:required=>true %>
<%= select_tag :product_id2, options_from_collection_for_select(#products.where.not(:name=>nil), 'id', 'name'), :include_blank => "Please select..." ,:required=>true %>
I am working with David Francisco's rails feedback plugin. The plugin works fine, except for one thing - I would need the feedback form to submit to the database the url for the page where the feedback form was used. Does anyone know how to do this?
The view that would need to send the current url, in addition to the currently sent information:
<h4>Feedback</h4>
<p>Please leave us feedback, it's really appreciated.</p>
<%= form_for #feedback, :as => :feedback, :url => feedback_index_path, :html => { :id => "feedback_form" } do |f| -%>
<%= f.hidden_field 'user_id', :value => current_user.id %>
<% unless #error_message.blank? %>
<p class="error">
<%=h #error_message %>
</p>
<% end %>
<p>
<%= f.label 'subject' %>
<%= f.select 'subject', ['Problem', 'Suggestion', 'Question', 'Other'] %>
</p>
<p>
<%= f.label 'comment' %><br />
<%= f.text_area 'comment', :rows => 10, :cols => 30 %>
</p>
<p><%= f.submit 'Send' %></p>
<% end -%>
Currently, feedback_index_path is always '/feedback', the url for the form.
Thank you,
Alexandra
You can use request.referer in your controller to get the path of the page that called your action. request.referer returns a full url but you can parse it with the URI module:
URI(request.referer).path
I see some people have suggested request.fullpath but this is actually the path of the action that's processing the request (in your case /feedbacks/new) and not the path where the form was submitted from.
If request.fullpath doesn't work incorrectly, you can make a quick hack, storing page url in data-attributes of page, and on submit get current url by jQuery from that attributes.
But it is a hack, just to make it working.
BTW how do you render feedback form?
feedback_index_path is a routes helper method that will always return the same thing. In your case /feedback.
Look here for info on accessing the current URL in both Rails 2 and 3.
I have been struggling with a problem in Rails for a couple of days and still could not find the solution. Could you help me with that?
Problem: I have a search box that puts a :search_string entry in the params structure. I use a form_tag for that and it works fine.
<% form_tag :controller=> 'items', :action => 'find' do %>
<%= text_field_tag :search_string, params[:search_string] %>
<% end %>
The problem is when I want to add and update other params key-value (in another view), for instance :start_date, to filter the search_string result. Here is the code snipped that I use in the view:
<% form_tag :controller=> "items", :action => "find", :params => params do %>
<%= hidden_field_tag :date_start, '2010-04-01' %>
<%= submit_tag 'April' %>
<% end %>
<% form_tag :controller=> "items", :action => "find", :params => params do %>
<%= hidden_field_tag :date_start, '2010-03-01' %>
<%= submit_tag 'March' %>
<% end %>
When I first click on "April" submit button, then the params is correctly passed to the controller (i.e. there is a params[:start_date]='April'). However when I try to click "March" button afterwards, the params[:start_date] is not updated. I definitely think this is a stupid newbie mistake, but I cannot figure out how to properly use the form_tag. Could you tell me if I am doing something work? Otherwise, could you advise me which is the best way to update the params using form_tag's ? Thank you very much in advance.
Miquel
What you may want to do is instead force-merge the parameters, something along the lines of:
<% form_tag :controller=> "items", :action => "find", :params => params.merge(:date_start => '2010-03-01') do %>
<%= submit_tag 'March' %>
<% end %>
There is a chance you're inadvertently submitting two of the same parameter and the first of them is getting picked, but since the "first" is not clearly defined, you may get inconsistent results.
Have a look in your log file to see what parameters are received from the two forms.
I am stuck one more time ... and one more time I suspect it's a stupid syntax problem:
I want to pass 2 vaiables in the url with my super simple search form.
I was expecting a URL like this:
http://mydomain/categories/search?search=pdf&os=2
But I get this:
http://mydomain/categories/search?search=pdf&os[]=
I thought it should work like this:
<% form_tag search_path, :method => 'get' do %>
<%= text_field_tag :search, params[:search] %>
<%= hidden_field :os, params[#category.id] %>
<%= submit_tag "Search", :name => nil %>
<% end %>
... but well, it didn't do it ...
Does anyone know where I am going wrong?
Thanks!
Val
You need to modify the line a bit, using hidden_field_tag:
<%= hidden_field_tag :os, :value => #category.id %>
See the hidden_field_tag documentation for more information.
<%= hidden_field :os, params[#category.id] %>
Is going to access a key in the params hash with #category.id, is there such a key? Looks like not, as its returning nil.
Seems like you want something to the effect of
<%= hidden_field :os, #category.id %>
One of the things I'm doing includes several links on the show view. For instance, I have a link (or button) for "Accepting", and another one for "Rejecting". Click on Accept, and the model updates the is_accepted field as true, click on Reject, and the is_accepted field is false.
Now, how best do I handle this? In ASP.NET, I would have simply created a LinkButton and written a handler, but Rails doesn't work that way, so I'm trying to figure out how to essentially replicate what a LinkButton would do.
Right now, I'm coding two forms on the same view, nearly identical, that look like this:
<%= form_for #thing do |f| %>
<%= hidden_field_tag 'thing[is_accepted]', '1' %>
<%= f.submit "Accept" %>
<% end %>
<%= form_for #thing do |f| %>
<%= hidden_field_tag 'thing[is_accepted]', '0' %>
<%= f.submit "Reject" %>
<% end %>
This feels weird to me, but I can't seem to find anything that says this is the wrong way to do it.
I could, I assume, dry things up by using a partial and/or a helper method, but I wanted to make sure I'm on the right track and not doing something totally wrongly.
You can give your submit tag a name.. ie
<%= form_for #thing do |f| %>
<%= hidden_field_tag 'thing[is_accepted]' %>
<%= f.submit "Accept", :name => 'accept' %>
<%= f.submit "Reject", :name => 'reject' %>
<% end %>
Then you can detect the name in params[] and skip the '1'/'0' value.
I think you're going about it the right way. One way to clean up your forms is by using the model form helpers all the way through, so you'd end up with something like
<%= form_for #thing do |f| %>
<%= f.hidden_field :accepted, :value => true %>
<%= f.submit "Accept" %>
<% end %>
<%= form_for #thing do |f| %>
<%= f.hidden_field :accepted, :value => false %>
<%= f.submit "Reject" %>
<% end %>
But other than that, it looks like the right way to go about it. I would suggest against creating new methods to do this, because you're not doing anything outside of normal web requests (updating a model in this instance).
Using the submit tag as the switch and detecting it in params[] is also a good way, but I usually prefer to keep my controllers as vanilla as possible. In the end, both of these ways would end up with the same amount of 'stuff' in the UI, so whichever style you'd rather use should be fine.
Depending on how you want your UI to work you might consider link_to_remote (part of the prototype helper) - you can specify an action, params etc, and have it return some JS that gets run.
If you're using map.resources in your routes.rb you should be able to do something like this:
map.resources :things, :member => {:accept => :get, :reject => :get}
Then in your controller:
def accept
#thing = Thing.find(params[:id])
#thing.is_accepted = true
#thing.save
end
def reject
#thing = Thing.find(params[:id])
#thing.is_accepted = false
#thing.save
end
And finally in your view:
<%= link_to 'Accept', accept_thing_url(#thing) %>
<%= link_to 'Reject', reject_thing_url(#thing) %>
Or if you are using Ajax:
<%= link_to_remote 'Accept', :url => accept_thing_url(#thing) %>
<%= link_to_remote 'Reject', :url => reject_thing_url(#thing) %>