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

时间:2012-01-06 23:32:19

标签: ruby-on-rails form-for

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

1 个答案:

答案 0 :(得分:8)

您是在谈论select还是select_tag还是collection_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]})