RoR select_tag default value & options

后端 未结 5 1724
隐瞒了意图╮
隐瞒了意图╮ 2020-12-13 23:38

How can I set a default value using select_tag, and how can I keep the options open on page load?

5条回答
  •  不思量自难忘°
    2020-12-14 00:13

    Its already explained, Will try to give an example for achieving the same without options_for_select

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

提交回复
热议问题