我正在尝试迭代我的Rails模型FlsCenter
,并在选择特定的ActiveRecord时执行一些简单的JavaScript。
如何使用f.select
之类的内容并生成此<select>
代码生成,而无需进入任何form_for
。我必须使用vanilla HTML吗?
我想写:
=f.select(:fls_center, FlsCenter.all.map{|p| [p.name, p.id]}, prompt: "Select a Program Center")
但在form_for
之外,没有相关f
。同样,当我使用form_tag
和简单=select
时,它无法达到预期效果。有什么想法吗?
答案 0 :(得分:3)
Rails'select_tag
与options_from_collection_for_select
结合应该可以解决问题:
select_tag('fls_center', options_from_collection_for_select(FlsCenter.all, 'name', 'id'), prompt: "Select a Program Center")
答案 1 :(得分:1)
所有*_tag
助手都是未受表单对象约束的助手,因此您可以使用select_tag
而不使用表单
select_tag :fls_center, options_from_collection_for_select(FlsCenter.all, :id, :name), prompt: "Select a Program Center"