如何在rails中传递复选框的多个值

时间:2013-10-29 19:48:39

标签: ruby-on-rails-3 haml

我有以下haml代码

%nav.breadcrumbs
  %h2 Breadcrumbs
  %ol
    %li
      %a{href: data_sources_path} #{t('Defaults.Home')}
    %li
      %a
    %li
      %em #{t('Registrations.new.AccessRequest')}
%br
%h3#content-header1.content-header #{t('Registrations.new.Header')}
%section.form
  %form.standard{action: registrations_path, method: 'post'}
    %fieldset{"aria-labelledby" => "content-header"}
      %input{type: 'hidden', name: 'authenticity_token', value: form_authenticity_token.to_s}  
      %legend  
      %ul
        %li    
          %label 
            #{t('Registrations.new.CheckBoxHeader')}
            %abbr.required{title: "Required"} *
        - Role::ROLES.each do |role| 
          %input#roles{name: "access", type: "checkbox",value:"#{role}"}/
          %label{for: "roles"} #{role}            
    %fieldset.input-actions
      %legend Actions
      %input.primary-action{name: "invoke", type: "submit", value: t('Buttons.Send')}/
      %input.cancel{name: "invoke", type: "submit", value: t('Buttons.Cancel')}/

这里假设我的角色是管理员和可编辑的...我可以成功地将角色显示为复选框,并且params [:access]为我提供了我选择的特定角色。假设我选择了两个角色params [:access]只返回admin。我想得到这两个角色。任何帮助,将不胜感激。感谢

1 个答案:

答案 0 :(得分:0)

你需要做一个简单的技巧让rails看到“access”作为一个数组

%input#roles{name: "access[]", type: "checkbox",value:"#{role}"}/

那样params [:access]将是一个可以在其上循环的数组