Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 878
Tom Maxwell Avatar asked Dec 09 '22 07:12

Tom Maxwell


1 Answers

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

like image 140
Monideep Avatar answered Feb 13 '23 11:02

Monideep