Rails 在 grouped_collection_select 中选择一个字段 [英] Rails selecting a field in grouped_collection_select
本文介绍了Rails 在 grouped_collection_select 中选择一个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在视图中使用此代码创建选择 grouped_collection_select(:query, :city_id, @states, :cities, :name, :id, :name, {:selected => "Chicago"})
看起来像这样:
I'm using this code in my view to create a selection grouped_collection_select(:query, :city_id, @states, :cities, :name, :id, :name, {:selected => "Chicago"})
that looks like this:
我想默认选择芝加哥".我怎样才能让它工作?
I want to have "Chicago" selected by default. How can I get this to work?
推荐答案
在上面的示例中,您可以通过定义芝加哥的选定键索引来选择芝加哥".
Hi on your sample above you can select "Chicago" by defining the selected key index of chicago.
这是一个例子:
@city_group =
[
["Wisoncin", [["Lake Geneva", "1"],
["Elkhart Lake", "2"]]],
["Michigan", [["Harbor Country", "3"], ["Traverse City", "4"]]],
["Indiana", [["Bloomington", "5"], ["Valparaiso", "6"]]],
["Minnesota", [["Twin Cities",
"7"], ["Bloomington", "8"], ["Stillwater",
"9"]]],
["Florida", [["Sanibel & Captiva", "10"]]],
["Illinois", [["Chicago", "11"],
["Galena", "12"]]],
]
并在您的视图中添加:
<%= select_tag(:brand_id, grouped_options_for_select(@city_group, selected_key = "11", prompt = nil)) %>
希望有帮助!享受!
这篇关于Rails 在 grouped_collection_select 中选择一个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文