2016-03-01 100 views
0

我有,我需要的形式,一个简单的文本字段的表单进行填写:必填字段上collection_select类型字段

<%= f.text_field :email, :required => true %> 

下一个字段是,我要强制用户到collection_select类型选择一个选项。我想:

<%= f.collection_select(:list_ids, List.where(user_id: current_user), :id, :name, {}, {multiple: true}), :required => true %> 

它给我的错误:

syntax error, unexpected ',', expecting ')' ..., :name, {}, {multiple: true}), :required => true);@output_... ...^

没有:required => true选项的代码工作正常。在这种情况下,我如何强制用户进行选择?由于

+0

的可能的复制[为什么?需要=>真不工作的收集\ _select(http://stackoverflow.com/questions/24767768/why-is - 需要 - 真的没有在收集选择) – SsouLlesS

+0

考虑选择我的答案作为接受的答案,这样社会上的其他人将帮助你,当你有更多的问题... – SsouLlesS

回答

1

尝试修改此

<%= f.collection_select(:list_ids, List.where(user_id: current_user), :id, :name, {}, {multiple: true}), :required => true %> 

这个

<%= f.collection_select :list_ids, List.where(user_id: current_user), :id, :name, {}, {multiple: true, required: true} %> 
1

试试这个

<%= f.collection_select(:list_ids, List.where(user_id: current_user), :id, :name, {}, {multiple: true, required: true}) %> 

说明:

根据Rails的文档山坳语法lection_select功能如下:

collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {}) 

按语法选项和html_options是哈希,所以你需要将它们封闭在大括号。

参考 - http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/collection_select

Credit

+0

你的答案被复制几乎行从这个[SO帖](http://stackoverflow.com/questions/24767768/why-is-required-true-not-working-on-collection-select/24767913#24767913)如果你要复制,你应该信用那个人回答 – MilesStanfield

+0

是的,你说得对,其实我正要把这个标记为dup – SsouLlesS

+0

谢谢你的帮忙。 – Jan