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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With