Collect to Select — collection_select in Ruby on Rails

Colten Appleby
3 min readApr 1, 2021

--

As a student of Ruby, I found myself looking up documentation for a Rails’ form_for method often. While form_for is being depreciated for form_with, I find myself defaulting to form_for due to my relative familiarity with it. While creating forms with form_for I I prefer to give the user drop menu options instead of free type which can cause issues if the user is unfamiliar with the program. Its hard to mess up a dropdown! That is where collection_select comes in handy!

At first I stumbled upon collection_select through a series of StackOverFlow answers to questions similar to mine: “How do I create a dropdown menu in Ruby on Rails?” After discovering this amazing method, I struggled to get a grasp on how to use it. Whenever I needed to create a dropdown menu in a form-for method I had to google the documentation and scour StackOverFlow to try to decipher collection_select. This is where this post comes from!

Here is how collection works with some real life examples. Hopefully this helps other rookie developers how to create dropdown menus in their Rails applications.

When to use collection_select?

Collection Select can be used to create dropdown menus in the form_for method found in Ruby on Rails.

Beer Example:

In our example we are going to create a review for a beer! We need to select the beer we are going to write the review about from a dropdown, give the beer a rating and write a blurb about the beer.

A quick refresher on form_for: Rails is inputting the users information into a class which then determines if the information should be updating an instance of an ActiveRecord table or creating a new one. This is the magic of Rails with a lot of the work done in the background out of sight. Form_for will make the determination about update vs create when given the object. In our case the object is @review. For more information Form_for please refer to the documentation here.

Below is the new method in the reviews_controller.rb file. As you can see we are creating a new instance of the Review model.

Lets break down the collection_select code:

  1. form_for method
  2. calling the collection_select method
  3. The object that form_for will save the dropdown selection. In this case we are selecting a beer from a list of all beers, taking the :id of the beer and storing it in the :beer_id. You will be able to access this by calling params[:beer_id] in the controller file.
  4. The options! In this case are giving the user the option to select any beer that has been created.
  5. This is what will be stored in :beer_id (number 3 on this list)
  6. This is what will be displayed to the user in the dropdown menu.

I hope this is as helpful for you as it was for me to create this walkthrough of collection_select. Collection_select is a very powerful tool that gives you user immense power to create and edit data while confining them to a strong set of rules.

**BONUS**

If we wanted to create a custom dropdown menu we could do the following:

--

--

Colten Appleby
Colten Appleby

Written by Colten Appleby

Student in immersive software development bootcamp

No responses yet