totocaster
totocaster

Reputation: 6363

Rails select helper - Default selected value, how?

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?

Upvotes: 201

Views: 221236

Answers (14)

random_user_0891
random_user_0891

Reputation: 2051

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

Upvotes: 1

Shelvacu
Shelvacu

Reputation: 4362

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}

Upvotes: 5

lynx
lynx

Reputation: 1491

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

<%= f.select :project_id, options_for_select(..., @work.project_id) %>

Upvotes: 149

shiva kumar
shiva kumar

Reputation: 11414

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

Upvotes: 1

danengle
danengle

Reputation: 566

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

Upvotes: 11

user2480972
user2480972

Reputation: 19

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 

Upvotes: 0

promacuser
promacuser

Reputation: 396

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

Upvotes: 0

totocaster
totocaster

Reputation: 6363

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?

Upvotes: 10

htanata
htanata

Reputation: 36944

This should do it:

<%= f.select :project_id, @project_select, :selected => params[:pid] %>

Upvotes: 244

swapnilp
swapnilp

Reputation: 491

<%= f.select :project_id, options_from_collection_for_select(@project_select,) %>

Upvotes: 3

gavit
gavit

Reputation: 509

Try this:

    <%= f.select :project_id, @project_select, :selected => f.object.project_id %>

Upvotes: 24

jschorr
jschorr

Reputation: 3054

<%= f.select :project_id, @project_select, :selected => params[:pid] %>

Upvotes: 7

Cheng
Cheng

Reputation: 4886

Rails 3.0.9

select options_for_select([value1, value2, value3], default)

Upvotes: 59

epochwolf
epochwolf

Reputation: 12772

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

Upvotes: -2

Related Questions