如何为 options_from_collection_for_select 中的每个选项动态生成唯一名称? [英] How do I dynamically generate a unique name for each option within an options_from_collection_for_select?

查看:61
本文介绍了如何为 options_from_collection_for_select 中的每个选项动态生成唯一名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想生成一个这样的选项标签:

I want to generate an option tag like this:

<option name="option_1" value="1">Small</option>
<option name="option_2" value="2">Medium</option>

像这样使用我的电话:

<%= select_tag option.name, options_from_collection_for_select(option.option_values, "id", "name"), include_blank: true %>

通过生成这个让我走到一半:

Gets me half-way there, by generating this:

<option value="1">Small</option>
<option value="2">Medium</option>

如何使用此结构将 name 属性添加到选项标记?这甚至可能吗?

How do I add the name attribute to the option tag, using this structure? Is that even possible?

推荐答案

select_tag option.name, options_for_select(option.option_values.map{ |o| [o.name, o.id, {:name_or_smth_else => "option_#{o.id}"}] })

options_for_select

这篇关于如何为 options_from_collection_for_select 中的每个选项动态生成唯一名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆