Rails select helper - Default selected value, how?
Ruby on-RailsRuby on-Rails Problem Overview
Here is a piece of code I'm using now:
<%= f.select :project_id, @project_select %>
How to modify it to make its default value equal to to params[:pid]
when page is loaded?
Ruby on-Rails Solutions
Solution 1 - Ruby on-Rails
This should do it:
<%= f.select :project_id, @project_select, :selected => params[:pid] %>
Solution 2 - Ruby on-Rails
Use the right attribute of the current instance (e.g. @work.project_id
):
<%= f.select :project_id, options_for_select(..., @work.project_id) %>
Solution 3 - Ruby on-Rails
Rails 3.0.9
select options_for_select([value1, value2, value3], default)
Solution 4 - Ruby on-Rails
The problem with all of these answers is they set the field to the default value even if you're trying to edit your record.
You need to set the default to your existing value and then only set it to the actual default if you don't have a value. Like so:
f.select :field, options_for_select(value_array, f.object.field || default_value)
For anyone not familiar with f.object.field
you always use f.object
then add your field name to the end of that.
Solution 5 - Ruby on-Rails
Try this:
<%= f.select :project_id, @project_select, :selected => f.object.project_id %>
Solution 6 - Ruby on-Rails
if params[:pid] is a string, which if it came from a form, it is, you'll probably need to use
params[:pid].to_i
for the correct item to be selected in the select list
Solution 7 - Ruby on-Rails
I've found solution and I found that I'm pretty unexperienced in RoR.
Inside the controller that manages view described above add this:
@work.project_id = params[:pid] unless params[:pid].nil?
Solution 8 - Ruby on-Rails
<%= f.select :project_id, @project_select, :selected => params[:pid] %>
Solution 9 - Ruby on-Rails
I couldn't get this to work and found that I needed to add the "selected" html attribute not only to the correct <option>
tag but also to the <select>
tag. MDN's docs on the selected attribute of the select tag say:
> selected - Boolean attribute indicates that a specific option can be initially selected.
That means the code should look like:
f.select :project_id, options_for_select(@project_select, default_val), html: {selected: true}
Solution 10 - Ruby on-Rails
<%= f.select :project_id, options_from_collection_for_select(@project_select,) %>
Solution 11 - Ruby on-Rails
Its already explained, Will try to give an example
let the select list be
select_list = { eligible: 1, ineligible: 0 }
So the following code results in
<%= f.select :to_vote, select_list %>
<select name="to_vote" id="to_vote">
<option value="1">eligible</option>
<option value="0">ineligible</option>
</select>
So to make a option selected by default we have to use selected: value.
<%= f.select :to_vote, select_list, selected: select_list.can_vote? ? 1 : 0 %>
if can_vote? returns true it sets selected: 1 then the first value will be selected else second.
select name="driver[bca_aw_eligible]" id="driver_bca_aw_eligible">
<option value="1">eligible</option>
<option selected="selected" value="0">ineligible</option>
</select>
if the select options are just a array list instead of hast then the selected will be just the value to be selected for example if
select_list = [ 'eligible', 'ineligible' ]
now the selected will just take
<%= f.select :to_vote, select_list, selected: 'ineligible' %>
Solution 12 - Ruby on-Rails
Mike Bethany's answer above worked to set a default value when a new record was being created and still have the value the user selected show in the edit form. However, I added a model validation and it would not let me submit the form. Here's what worked for me to have a model validation on the field and to show a default value as well as the value the user selected when in edit mode.
<div class="field">
<%= f.label :project_id, 'my project id', class: "control-label" %><br>
<% if @work.new_record? %>
<%= f.select :project_id, options_for_select([['Yes', true], ['No', false]], true), {}, required: true, class: "form-control" %><br>
<% else %>
<%= f.select :project_id, options_for_select([['Yes', true], ['No', false]], @work.project_id), {}, required: true, class: "form-control" %><br>
<% end %>
</div>
model validation
validates :project_id, presence: true
Solution 13 - Ruby on-Rails
Alternatively, you could set the :project_id attribute in the controller, since the first argument of f.select pulls that particular attribute.
Solution 14 - Ruby on-Rails
If try to print the f object, then you will see that there is f.object that can be probed for getting the selected item (applicable for all rails version > 2.3)
logger.warn("f #{f.object.inspect}")
so, use the following script to get the proper selected option:
:selected => f.object.your_field
Solution 15 - Ruby on-Rails
This should work for you. It just passes {:value => params[:pid] }
to the html_options variable.
<%= f.select :project_id, @project_select, {}, {:value => params[:pid] } %>