如何在Rails中使用form_for中的select方法

Joh*_*ohn 1 ruby-on-rails form-for

我想在Rails中使用form_for中的select方法.我不太清楚该怎么做.谁能指点我一个例子?

set*_*rgo 8

你在谈论selectselect_tagcollection_select

# advanced users only
= f.select :field_id, options_for_select(Model.collect{|m| [m.name, m.id]})

# easiest, assuming you have a model
= f.collection_select :field_id, Model.all, :id, :name

# without model
= select_tag 'model[field_id]', @model.field_id, options_for_select(Model.collect{|m| [m.name, m.id]})
Run Code Online (Sandbox Code Playgroud)

  • 由于他正在使用form_for,我猜猜选择是正确的.它的文档在这里(你只是不将对象作为第一个参数传递,因为它是由表单对象推断的):http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method -i选 (2认同)