Why is :required => true not working on collection_select?

ruby, ruby-on-rails

Solution

Try this

<%= f.collection_select(:category_id, Category.all, :id, :name, {:prompt => 'Choose a category'}, {:required => true}) %>

Explanation:

According to the Rails documentation the syntax for the `collection_select` function looks like this:

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

As per the syntax `options` and `html_options` are hashes, so you need to enclose them in braces.

Reference - http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/collection_select

Problem

I want to make sure the user selects a category in my form before they can submit it, but `:required => true` doesn't seem to be working. Here's the select: ``` <%= f.collection_select :category_id, Category.all, :id, :name, :prompt => 'Choose a category' %> ``` Any advice?

Original source