我想确保用户在提交之前在我的表单中选择一个类别,但是:required => true
似乎不起作用。这是选择:
<%= f.collection_select :category_id, Category.all, :id, :name, :prompt => 'Choose a category' %>
有什么建议吗?
Try this
<%= 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
and html_options
是哈希值,因此您需要将它们括在大括号中。
参考 -http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/collection_select http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/collection_select
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)