collection_select 类型字段的必填字段
Required Field on collection_select type field
我有一个带有简单文本字段的表单,我需要在其中填写表单:
<%= f.text_field :email, :required => true %>
下一个字段是 collection_select 类型,我想在其中强制用户 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
选项代码工作正常。在这种情况下,我该如何强制用户使用 selection?谢谢
尝试改变这个
<%= 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} %>
试试这个
<%= f.collection_select(:list_ids, List.where(user_id: current_user), :id, :name, {}, {multiple: true, required: true}) %>
解释:
根据 Rails 文档,collection_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
<%= form.collection_select :msr_cd,
@msrs, :msr_cd, :msr_cd,
{multiple: false, required: true},
data: { placeholder: "Select Measure" },
class: "form-control col-sm-12 chosen-select"
%>
Note:
@msrs from the controller
:msr_cd - option value`enter code here`
:msr_cd - option text
We can pass the chosen select like above
我有一个带有简单文本字段的表单,我需要在其中填写表单:
<%= f.text_field :email, :required => true %>
下一个字段是 collection_select 类型,我想在其中强制用户 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
选项代码工作正常。在这种情况下,我该如何强制用户使用 selection?谢谢
尝试改变这个
<%= 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} %>
试试这个
<%= f.collection_select(:list_ids, List.where(user_id: current_user), :id, :name, {}, {multiple: true, required: true}) %>
解释:
根据 Rails 文档,collection_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
<%= form.collection_select :msr_cd,
@msrs, :msr_cd, :msr_cd,
{multiple: false, required: true},
data: { placeholder: "Select Measure" },
class: "form-control col-sm-12 chosen-select"
%>
Note:
@msrs from the controller
:msr_cd - option value`enter code here`
:msr_cd - option text
We can pass the chosen select like above