Ruby-on-rails RoR select_tag 默认值和选项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3396025/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:59:41  来源:igfitidea点击:

RoR select_tag default value & options

ruby-on-rails

提问by nirmal

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

如何使用 设置默认值select_tag,以及如何在页面加载时保持选项打开?

回答by klew

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

如果您在select_tag没有任何其他帮助程序的情况下使用,那么您可以在 html 中进行:

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

Or with options_for_select:

或与options_for_select

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

Or with options_from_collection_for_select:

或与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_tagdoc, options_for_selectdocand from options_from_collection_for_selectdoc.

示例来自select_tagdocoptions_for_selectdoc和来自options_from_collection_for_selectdoc

回答by Gabriel Lucuy

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.

在 Rails 5 中效果很好。

回答by Ni3

For options_for_select

对于 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.

请注意,您指定的选定值必须是 value 类型。即如果值是整数格式,则选择的值参数也应该是整数。

回答by shiva kumar

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

它已经解释过了,将尝试举一个例子来实现没有 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.

所以要默认选择一个选项,我们必须使用selected: value

<%= f.select :to_vote, select_list, selected: select_list.can_vote? ? 1 : 0 %>

if can_vote?returns true it sets selected: 1then the first value will be selected else second.

如果can_vote?返回 true 它设置selected: 1那么第一个值将被选择,否则第二个。

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

如果选择选项只是一个数组列表而不是 hast 那么所选的将只是要选择的值,例如如果

select_list = [ 'eligible', 'ineligible' ]

now the selected will just take

现在选定的将只需要

<%= f.select :to_vote, select_list, selected: 'ineligible' %>

回答by montrealmike

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