为什么是 :required =>真的不在 collection_select 上工作吗? [英] Why is :required => true not working on collection_select?
本文介绍了为什么是 :required =>真的不在 collection_select 上工作吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想确保用户在提交之前在我的表单中选择了一个类别,但是 :required =>true
似乎不起作用.这是选择:
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' %>
有什么建议吗?
推荐答案
试试这个
<%= f.collection_select(:category_id, Category.all, :id, :name, {:prompt => 'Choose a category'}, {:required => true}) %>
说明:
根据 Rails 文档,collection_select
函数的语法如下所示:
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 = {})
根据语法,options
和 html_options
是散列,因此您需要将它们括在大括号中.
As per the syntax options
and html_options
are hashes, so you need to enclose them in braces.
参考 - http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/collection_select
这篇关于为什么是 :required =>真的不在 collection_select 上工作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文