Rails helpers for drop downs were initially confusing to me. I didn’t get how to deal with all the options and variations.

Once I got a few examples going however, they didn’t see that bad. Here’s what I have learned.

collection_select

<%= collection_select(:category, :id, Category.all, :id, :name) %>

produces

<select id="category_id" name="category[id]">
   <option value="1">art</option>
   <option value="2">books</option>
   <option value="3">computers</option>
</select>

This one is handy when you know what your resource is (in this case categories) and you just want to populate it into a drop down.

collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})

object – is basically the rails resource you are going to get the id and value from for each of your drop down options

method – is the way rails gets an instance of your object. It calls this ‘method’ and whatever object is returned is the instance it uses to populate the drop down options.

collection – is the resource you are going to use to populate the drop down. You can either call it here (Category.all) or have it populated in a controller and have it set as an instance variable (i.e. @categories = Category.all in your controller).

value_method & text_method – are the methods called on your object instance to populate the value and text elements of the drop down.

If you wanted to set some options you can also:

collection_select(:category, :id, Category.all, :id, :name,  {:include_blank => "Please select"}, {:class=>'my-custom-class'})

How do I extract selected drop down value on the backend?

One handy way to see what values are being passed to your controller is to raise an exception and inspect the parameters:

class MyController  < ApplicationController
    def index
      raise params.inspect
      ...
    end
end

When you do this for the above example you see the parameters come to us looking like this:

So we have a category hash with includes the name/value pair id => 1.

To extract that in our view we’d go:

      id = params[:category][:id]

Which you could check by going:

      raise id.inspect

What if my drop down is part of a form post?

Same thing as above. Only this time your drop down is going to be tied to the resource you are posting as.

<%= form_for @listing do |f| %>
  <div class="field">
    <%= f.label :category %><br />
    <%= f.collection_select(:category_id, @categories, :id, :name, :include_blank => "Please select") %>
  </div>
  <div class="actions">
    <%= f.submit "Submit" %>
  </div>
<% end %>

Here that f.collection_select puts the parameters into a listing hash which looks something like this:

Same thing, only now the select drop down value is associated with the posting (listing) hash which you can extract as”

params[:listing][category_id]

For more information on rails drop downs checkout:

collection_select api
http://guides.rubyonrails.org/form_helpers.html#making-select-boxes-with-ease

About these ads