Rails 在 grouped

编程入门 行业动态 更新时间:2024-10-26 02:24:58
本文介绍了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

本文发布于:2023-10-17 06:16:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1500013.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Rails   grouped

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!