为什么:required => true不能在collection_select上工作?
我想确保用户在提交之前在我的表单中选择一个类别,但是:required => true
似乎不起作用。 这是选择:
'Choose a category' %>
任何建议?
试试这个
<%= f.collection_select(:category_id, Category.all, :id, :name, {:prompt => 'Choose a category'}, {:required => true}) %>
说明:
根据Rails文档, collection_select
函数的语法如下所示:
collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})
根据语法options
和html_options
是哈希值,因此您需要将它们括在大括号中。
参考 – http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/collection_select