Rails form_for with collection_select

Rym*_*mo4 13 ruby-on-rails ruby-on-rails-3

我正在尝试创建一个创建Ranking类实例的字段.它有一个注释字段已设置params[:ranking][:comment]但现在我想添加一个下拉列表,显示如下内容:

1:可怕,2:差,3:平庸,4:好,5:太棒了

我希望这些将params [:ranking] [:score]设置为1-5,这样在我的create方法中我可以这样做:

 @ranking = Ranking.new( #....
                        :score => params[:ranking][:score])
Run Code Online (Sandbox Code Playgroud)

我的表单现在看起来像这样:

<%= form_for([@essay, @ranking]) do |f| %>
  <%= render 'shared/error_messages', :object => f.object %>
  <div classs="field">
    <%= f.text_area :comment %>
  </div>
  <div classs="field">
      <%= #something here!%>
  </div>
  <div class="actions">
    <%= f.submit "Submit" %>
  </div>
<% end %>
Run Code Online (Sandbox Code Playgroud)

我知道我需要使用它,collection_select但我无法让它工作.

Aar*_*nni 60

您应该只能使用常规select帮助程序:

f.select :score, [['horrible', 1], ['poor', 2], ['mediocre', 3], ['good', 4], ['great', 5]]
Run Code Online (Sandbox Code Playgroud)

collection_select如果您有分数模型,您可以使用.就像是:

f.collection_select :score_id, Score.all, :id, :name
Run Code Online (Sandbox Code Playgroud)

请参阅collection_select的API文档