I''m having some trouble with this select tag. I have a simple search form that passes parameters with GET. I have the search by name working fine but want to add a drop down list to search by classification as well. here is what i have: <% form_tag items_path, :method => ''get'' do %> <table> <tr><%= link_to ''New Item'', new_item_path %><td></tr> <tr><td><%= text_field_tag :search, params[:search] %></td> <td><%= select("class","id", @classifications.map {|u| [u.name,u.id]}) %> <td><%= submit_tag "Search", :name => nil %></td></tr> </table> <% end %> it works but the url looks like: http://domain.com/items?search=&class%5Bid%5D=1 because it is sending class[id]=1 i would like it to send just class=1 (or whatever the classification id is) also, is there a good way to add a :prompt (although i don''t think this is possible with just a select tag) so if a user does not select a classification then it will not pass a GET parameter. thanks! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Guess you could do it like this: <%= select("class","id", @classifications.map {|u| [u.name,u.id]}, {:prompt => ''Select an option''}) %> On Jul 19, 3:33 am, Scott Kulik <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> I''m having some trouble with this select tag. > > I have a simple search form that passes parameters with GET. I have the > search by name working fine but want to add a drop down list to search > by classification as well. > > here is what i have: > > <% form_tag items_path, :method => ''get'' do %> > <table> > <tr><%= link_to ''New Item'', new_item_path %><td></tr> > <tr><td><%= text_field_tag :search, params[:search] %></td> > <td><%= select("class","id", @classifications.map {|u| [u.name,u.id]}) > %> > <td><%= submit_tag "Search", :name => nil %></td></tr> > </table> > <% end %> > > it works but the url looks like:http://domain.com/items?search=&class%5Bid%5D=1 > > because it is sending class[id]=1 > > i would like it to send just class=1 (or whatever the classification id > is) > > also, is there a good way to add a :prompt (although i don''t think this > is possible with just a select tag) so if a user does not select a > classification then it will not pass a GET parameter. > > thanks! > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
nas wrote:> Guess you could do it like this: > > <%= select("class","id", @classifications.map {|u| [u.name,u.id]}, > {:prompt => ''Select an option''}) %> > > > On Jul 19, 3:33�am, Scott Kulik <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>thanks the prompt will work. one thing i am having trouble with now though is evaluating the params: if not params[:class][:id].blank? @items = Item.search(params[:search], params[:class][:id], params[:page]) else @items = Item.search(params[:search], "", params[:page]) end i''m getting a null error on the first line when i don''t have any params. how can i check to see if the params exist? i tried many combinations of things but i''m not sure how to do this. also, like i originally mentioned...if someone knows how i can use the collection select to pass params[:class] instead of params[:class][:id] that would be great! thanks! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
answer to your first question is: if !(params[:class] && params[:class][:id].blank?) @items = Item.search(params[:search], params[:class][:id], params[:page]) else @items = Item.search(params[:search], "", params[:page]) end for your question, I assume you want something like http://domain.com/items?search=&class=1 to achieve that you could use the select_tag like so: select_tag(:class, options_for_select(@classifications.map {|u| [u.name,u.id]})) hope that helps http://www.sphred.com http://www.nasir.wordpress.com On Jul 21, 10:34 pm, Scott Kulik <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> nas wrote: > > Guess you could do it like this: > > > <%= select("class","id", @classifications.map {|u| [u.name,u.id]}, > > {:prompt => ''Select an option''}) %> > > > On Jul 19, 3:33�am, Scott Kulik <rails-mailing-l...@andreas-s.net> > > thanks the prompt will work. > > one thing i am having trouble with now though is evaluating the params: > > if not params[:class][:id].blank? > @items = Item.search(params[:search], params[:class][:id], > params[:page]) > else > @items = Item.search(params[:search], "", params[:page]) > end > > i''m getting a null error on the first line when i don''t have any params. > how can i check to see if the params exist? i tried many combinations > of things but i''m not sure how to do this. > > also, like i originally mentioned...if someone knows how i can use the > collection select to pass params[:class] instead of params[:class][:id] > that would be great! > > thanks! > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
nas wrote:> answer to your first question is: > > if !(params[:class] && params[:class][:id].blank?) > @items = Item.search(params[:search], params[:class][:id], > params[:page]) > else > @items = Item.search(params[:search], "", params[:page]) > end > > for your question, I assume you want something like > http://domain.com/items?search=&class=1 > > to achieve that you could use the select_tag like so: > select_tag(:class, options_for_select(@classifications.map {|u| > [u.name,u.id]})) > > hope that helps > > http://www.sphred.com > http://www.nasir.wordpress.com > > On Jul 21, 10:34 pm, Scott Kulik <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>ah ok thanks i got it working like this since i wanted to add a prompt: <%= select_tag(:classification_id, ''<option value="">->Base/Class</option>'' + options_from_collection_for_select(@classifications, :id, :name)) %> the only issue i''m running into now is how can i get the select box to select the same value after the form is submitted? i know how to do this with collection_select but not sure it is possible with select_tag. can you do something similar to the following (this doesn''t work)? <%= select_tag(params[:classification_id], :classification_id, ''<option value="">->Base/Class</option>'' + options_from_collection_for_select(@classifications, :id, :name)) %> thanks for the help so far! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
i got it! <%= select_tag(:classification_id, ''<option value="">->Base/Class</option>'' + options_for_select(@classifications.collect{|classification| [classification.name, classification.id]}, params[:classification_id].to_i)) %> thanks nas! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---