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:
我想确保用户在提交表单之前选择了一个类别,但是:required => true似乎不起作用。这里的选择:
<%= f.collection_select :category_id, Category.all, :id, :name, :prompt => 'Choose a category' %>
Any advice?
任何建议吗?
1 个解决方案
#1
10
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:
根据Rails文档,collection_select函数的语法如下所示:
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.
根据语法选项和html_options是散列,所以需要将它们括在大括号中。
Reference - http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/collection_select
引用——http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/collection_select
#1
10
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:
根据Rails文档,collection_select函数的语法如下所示:
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.
根据语法选项和html_options是散列,所以需要将它们括在大括号中。
Reference - http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/collection_select
引用——http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/collection_select