197

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?

Arslan Ali
  • 17,418
  • 8
  • 58
  • 76
totocaster
  • 6,243
  • 5
  • 37
  • 46
  • I'm not sure whether you want a default project selected when the page is loaded, if :project_id isn't set, or if you want to return the value of the select in the params hash as :pid. – Tilendor Mar 08 '09 at 17:32

14 Answers14

239

This should do it:

<%= f.select :project_id, @project_select, :selected => params[:pid] %>
htanata
  • 36,666
  • 8
  • 50
  • 57
145

Use the right attribute of the current instance (e.g. @work.project_id):

<%= f.select :project_id, options_for_select(..., @work.project_id) %>
installero
  • 9,096
  • 3
  • 39
  • 43
lynx
  • 1,451
  • 1
  • 9
  • 2
59

Rails 3.0.9

select options_for_select([value1, value2, value3], default)
Cheng
  • 4,816
  • 4
  • 41
  • 44
  • I couldn't figure this solution out until I noticed your closing bracket around the values array **BEFORE** the comma that separates your default value... I missed this bracket here `value3], default` – BigRon Apr 04 '15 at 16:57
  • I tried a half dozen answers and passing the default to options_for_select was the only thing that worked for me. – Jason L. Dec 02 '21 at 15:23
24

Try this:

    <%= f.select :project_id, @project_select, :selected => f.object.project_id %>
gavit
  • 509
  • 3
  • 6
11

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

Adobe
  • 12,967
  • 10
  • 85
  • 126
danengle
  • 566
  • 4
  • 9
  • This answer helped me pinpoint my issue. My select box is dynamic and pulling a collection from the database. I was trying to populate the default selection by a title, not an id of the item. This answer helped me see that issue, and once I populated my `@project` variable with an id from the database table, instead of a title, this code worked appended onto the back of the `form.select` helper: `selected: @project` Thank you @danengle – Christopher Warrington Jan 27 '18 at 22:17
10

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?
Tsagadai
  • 887
  • 2
  • 8
  • 21
totocaster
  • 6,243
  • 5
  • 37
  • 46
7
<%= f.select :project_id, @project_select, :selected => params[:pid] %>
chown
  • 51,908
  • 16
  • 134
  • 170
jschorr
  • 3,034
  • 1
  • 17
  • 20
5

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}
Shelvacu
  • 4,245
  • 25
  • 44
3
<%= f.select :project_id, options_from_collection_for_select(@project_select,) %>
Bo Persson
  • 90,663
  • 31
  • 146
  • 203
swapnilp
  • 491
  • 2
  • 6
  • 9
1

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' %>
shiva kumar
  • 11,294
  • 4
  • 23
  • 28
1

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
random_user_0891
  • 1,863
  • 3
  • 15
  • 39
0

Alternatively, you could set the :project_id attribute in the controller, since the first argument of f.select pulls that particular attribute.

promacuser
  • 374
  • 1
  • 15
0

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

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] } %>
epochwolf
  • 12,340
  • 15
  • 59
  • 70