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?
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?
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.
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}
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' %>
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