select
select(object, method, choices, options = {}, html_options = {})
public
Create a select tag and a series of contained option tags for the provided object and method. The option currently held by the object will be selected, provided that the object is available. See options_for_select for the required format of the choices parameter.
Example with @post.person_id => 1:
select("post", "person_id", Person.find(:all).collect {|p| [ p.name, p.id ] }, { :include_blank => true })
could become:
<select name="post[person_id]"> <option value=""></option> <option value="1" selected="selected">David</option> <option value="2">Sam</option> <option value="3">Tobias</option> </select>
This can be used to provide a default set of options in the standard way: before rendering the create form, a new model instance is assigned the default options and bound to @model_name. Usually this model is not saved to the database. Instead, a second model object is created when the create request is received. This allows the user to submit a form page more than once with the expected results of creating multiple records. In addition, this allows a single partial to be used to generate form inputs for both edit and create forms.
By default, post.person_id is the selected option. Specify :selected => value to use a different selection or :selected => nil to leave all options unselected.
Demo: select onchange invoke an ajax
select(“order”, “customer_id”, o.customer.collect {|c| [ c.label, c.id ] },
{:include_blank => true, :selected => o.customer_id }, :onchange => remote_function(:update => "message_id", :method => "put", :with => "'item=' + value", :url => { :controller => :orders, :action => :set_customer_id, :id => order.id}))
Prompt vs. Select
According to the docs in form_options_helper.rb
:include_blank - set to true or a prompt string if the first option element of the select element is a blank. Useful if there is not a default value required for the select element.
:prompt - set to true or a prompt string. When the select element doesn’t have a value yet, this prepends an option with a generic prompt – “Please select” – or the given prompt string.
The main difference is that if the select already has a value, then :prompt will not show whereas the :include_blank always will.
Add empty option and text is -select-
select :object, :method, options, :prompt => ‘-select-’
Refactoring excessive code for selects
@garg: It is not recommended to have excessive code in the views. You should refactor your code a bit.
<%= f.select(:manufacturer_id, Manufacturer.find(:all).collect {|u| [u.name, u.id]}, :prompt => 'Select') %>
could be changed to this:
# in app/helpers/manufacturer_helper.rb def manufacturers_for_select Manufacturer.all.collect { |m| [m.name, m.id] } end # in the view <%= f.select(:manufacturer_id, manufacturers_for_select, :prompt => 'Select') %>
I would look into collection_select though:
<%= f.collection_select(:manufacturer_id, Manufacturer.all, :id, :name, :prompt => 'Select') %>
It’s much more clean and you don’t have to define a helper for it to be readable (altough it’s still quite long).
If you have to do this often, you should define a FormBuilder extension, so you get methods like f.manufacturer_select:
<%= f.manufacturer_select(:manufacturer_id, Manufacturer.all) %>
IMO, most projects should have a custom form builder anyway, so the addition would be very small. This is my personal opinion, so you don’t have to listen to it. :-)
":prompt" doesn't work
This does not work:
select :object, :method, options, :prompt => ’-select-’
This does work:
select :object, :method, {:include_blank => ’-select-’}
When using enumerables
When using enumerables and storing them as strings in the database don’t forget to use .to_s or the select helper won’t automatically select your choice when viewing your data after save.
Exemple:
dates = 1900..Date.today.year f.select(:year, dates.collect {|d| [d.to_s,d.to_s]}, {:include_blank => "Select"}, {:class => "some_class"} )
style for select
<%= select(“post”, “person_id”, Person.all.collect {|p| [ p.name, p.id ] }, {}, :style => “width:100px” %>
Using custom object via :object
Sometimes you need use select not only with @object as by default. For example if you have helper method like :
def select_parent_for(page) select(:page, :parent_id, Page.all.collect{|p| [p.name, p.id]} ) # <--- mistake! end
In selected line you will use @page instead parameter of method page.
The options has parameter :object (and all form helpers has such parameter)
Solution:
def select_parent_for(page) select(:page, :parent_id, ..., :object => page) end
Belongs_to, Has_many association
I have a belongs_to, has_many association between manufacturer and modelname. (manufacturer has many modelnames).
In the new modelname page, I have a drop down menu that lists all the manufacturers, so I choose eg Dell, and then in modelname.name field I enter inspiron or what ever. To create that drop down I used:
<%= f.select(:manufacturer_id, Manufacturer.find(:all).collect {|u| [u.name, u.id]}, :prompt => 'Select') %>
The reason that works was explained by fcheung and rsl on #rubyonrails. Thanks:
the form builder basically calls the method whose docs you have read, inserting the appropriate first argument
The object is referenced internally when you call f.whatever. in any of those tags, the object is omitted when used on a form block variable
Different Options
The docs don’t give any detail to what options are available, so I dug around and I think the only to options are :prompt and :include_blank
@Mange
If you’re actually looking to cut down on code why not use .map instead of the longer .collect?
Update for Rails 4
In the example
select("post", "person_id", Person.all.collect {|p| [ p.name, p.id ] }, { include_blank: true })
It can be updated to
select("post", "person_id", Person.pluck(:name, :id), { include_blank: true })
You could pass a range as the choices parameter
select :object, :method, (1..5)
not submitted even if non-multiple in certain cases.
As a note, just for followers, even if a select is not “multiple” if it is “unselected” (ex: in javascript you can set its value like document.getElementById(‘select_id’).value = ‘a non option’; ). And if that form is submitted, the browser also seems to not send anything about it to the server. So it’s for non-multiples as well, just this case is rare since typically the select will default to its “first value” and then users can only change it another known value, so typically you won’t run into that. This isn’t related to rails but thought I’d mention it.