如何在collection_select类型字段上添加必需的true

时间:2019-04-01 12:10:39

标签: ruby-on-rails ruby haml

我已经尝试在collection_select字段上添加required => true和:prompt =>“ select ...”,但是每次遇到语法错误时,我都会尝试添加。如何解决以下错误。

.field
  = f.label "Receiver"
  = f.collection_select(:receiver_admin_id, 
        Admin.agent_and_admin(current_admin.id, current_admin.parent_master_agent_id), 
        :id, 
        :agent_name, 
        :prompt => 'Select receiver', 
        {multiple: true}), 
      :required => true
  = f.collection_select(:receiver_admin_id, 
        Admin.agent_and_admin(current_admin.id, current_admin.parent_master_agent_id), 
        :id, 
        :agent_name, 
        :prompt => 'Select receiver', 
        {multiple: true, required: true})

  = f.collection_select(:receiver_admin_id, 
        Admin.agent_and_admin(current_admin.id, current_admin.parent_master_agent_id), 
        :id, 
        :agent_name, 
        :prompt => 'Select receiver',
        {}, 
        {multiple: true}), 
      :required => true
  = f.collection_select :receiver_admin_id, 
        Admin.agent_and_admin(current_admin.id, current_admin.parent_master_agent_id), 
        :id, 
        :agent_name, 
        :prompt => 'Select receiver', 
        {multiple: true}, 
        :required => true

2 个答案:

答案 0 :(得分:0)

请尝试以下操作:

  = f.collection_select :receiver_admin_id, Admin.agent_and_admin(current_admin.id, current_admin.parent_master_agent_id), :id, :agent_name, {prompt: 'Select receiver'}, {multiple: true, required: true}

答案 1 :(得分:0)

您几乎每次都关闭:)在文档in the rails guide中,您有:

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

使用表单助手(f.)意味着该对象(第一个参数)已得到处理,因此您只剩下:

method, collection, value_method, text_method, options, html_options

倍数和必需都在html_options中,因此:

= f.collection_select(:receiver_admin_id, Admin.agent_and_admin(current_admin.id, current_admin.parent_master_agent_id), :id, :agent_name, { :prompt => 'Select receiver' }, multiple: true, :required => true)