Ruby-on-rails – Rails form_for with collection_select


I'm trying to create a field that creates an instance of the Ranking class. It has a comment field already which sets the params[:ranking][:comment] but now I want to add a drop down that displays something like:

1: horrible, 2: poor, 3: mediocre, 4: good, 5: great

I would like these to set the params[:ranking][:score] to a value 1-5 so that in my create method I can do something like this:

 @ranking = #....
                        :score => params[:ranking][:score])

My form looks like this right now:

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

I know that I need to use the collection_select but I haven't been able to get it working.

Best Solution

You should just be able to use the regular select helper for something like that: :score, [['horrible', 1], ['poor', 2], ['mediocre', 3], ['good', 4], ['great', 5]]

You would use collection_select if you had a model for the scores. Something like:

f.collection_select :score_id, Score.all, :id, :name

See the API docs for collection_select