Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 = Ranking.new( #....
                        :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>
  <div classs="field">
      <%= #something here!%>
  </div>
  <div class="actions">
    <%= f.submit "Submit" %>
  </div>
<% end %>

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

like image 972
Rymo4 Avatar asked May 31 '11 17:05

Rymo4


1 Answers

You should just be able to use the regular select helper for something like that:

f.select :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

like image 197
Aaron Hinni Avatar answered Sep 28 '22 09:09

Aaron Hinni