一种在没有form_for或form_tag的情况下使用Rails表单助手的方法

时间:2015-04-16 16:42:45

标签: ruby-on-rails ruby

我正在尝试迭代我的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时,它无法达到预期效果。有什么想法吗?

2 个答案:

答案 0 :(得分:3)

Rails'select_tagoptions_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"