Rails select helper - Default selected value, how?

后端 未结 15 1725
臣服心动
臣服心动 2020-11-29 16:06

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

15条回答
  •  误落风尘
    2020-11-29 16:44

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

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

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

提交回复
热议问题