Select drop down list

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 %>

<%= link_to 'New Item', new_item_path %>
<%= text_field_tag :search, params[:search] %> <%= select("class","id", @classifications.map {|u| [u.name,u.id]}) %> <%= submit_tag "Search", :name => nil %>
<% end %>

it works but the url looks like:
http://domain.com/items?search=&class[id]=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!

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 K. [email protected]

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 K. [email protected]

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!

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

On Jul 21, 10:34Â pm, Scott K. [email protected]

i got it!

<%= select_tag(:classification_id, ‘->Base/Class’ +
options_for_select(@classifications.collect{|classification|
[classification.name, classification.id]},
params[:classification_id].to_i)) %>

thanks nas!

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 K. [email protected]

ah ok thanks i got it working like this since i wanted to add a prompt:

<%= select_tag(:classification_id, ‘->Base/Class’ +
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, ‘->Base/Class’ +
options_from_collection_for_select(@classifications, :id, :name)) %>

thanks for the help so far!