RoR select_tag default value & options

后端 未结 5 1715
隐瞒了意图╮
隐瞒了意图╮ 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:09

    For options_for_select

    <%= select_tag("products_per_page", options_for_select([["20",20],["50",50],["100",100]],params[:per_page].to_i),{:name => "products_per_page"} ) %>
    

    For options from collection for select

    <%= select_tag "category","<option value=''>Category</option>" +  options_from_collection_for_select(@store_categories, "id", "name",params[:category].to_i)%>
    

    Note that the selected value which you are specifying must be of type value. i.e. if value is in integer format then selected value parameter should also be integer.

    0 讨论(0)
  • 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 %>
    
    <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' %>
    
    0 讨论(0)
  • 2020-12-14 00:23

    Try this:

    <%= select_tag(:option, options_for_select([["Option 1",1],["Option 2",2],["Option 3",3]], params[:option] ), class:"select") %>
    

    works great in rails 5.

    0 讨论(0)
  • 2020-12-14 00:30

    another option (in case you need to add data attributes or other)

    = content_tag(:select) do
      - for a in array
        option data-url=a.url selected=(a.value == true) a.name
    
    0 讨论(0)
  • 2020-12-14 00:31

    If you are using select_tag without any other helper, then you can do it in html:

    select_tag "whatever", "<option>VISA</option><option selected=\"selected\">MasterCard</option>"
    

    Or with options_for_select:

    select_tag "whatever", options_for_select([ "VISA", "MasterCard" ], "MasterCard")
    

    Or with options_from_collection_for_select:

    select_tag [SELECT_FIELD_NAME], options_from_collection_for_select([YOUR_COLLECTION], [NAME_OF_ATTRIBUTE_TO_SEND], [NAME_OF_ATTRIBUTE_SEEN_BY_USER], [DEFAULT_VALUE])
    

    Example:

    select_tag "people", options_from_collection_for_select(@people, 'id', 'name', '1')
    

    Examples are from select_tag doc, options_for_select doc and from options_from_collection_for_select doc.

    0 讨论(0)
提交回复
热议问题