Ruby on Rails 4 选择多个 [英] Ruby on Rails 4 select multiple
问题描述
我有一个创建新用户的表单.我正在尝试添加一个下拉选项来选择权限级别.我希望能够为每个用户选择多个权限级别.
I have a form that creates new users. I am trying to add a drop down option to select permission levels. I want to be able to select multiple permission levels per user.
这是我的观点,我添加了 {:multiple =>真}
:
This is my view, I added {:multiple => true}
:
<%= f.label :permission, "Permission Level" %>
<%= f.select :permission, [ ["Read Only", "read"], ["IP Voice Telephony", "ip_voice"], ["IP Video Telephony", "ip_video_telephony"], ["Enterprise Gateways", "enterprise_gateways"], ["Consumer ATAs", "consumer_atas"], ["IP PBX", "ip_pbx"], ["Master of All", "all"] ], {prompt: "Select Permission Level"}, {:multiple => true}, class: "input-lg" %>
我的控制器,我添加了 :permission =>[]
:
My controller, I added :permission => []
:
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation, :admin, :permission => [])
end
我的视图错误,f.select:
The error I get for my view, f.select:
wrong number of arguments (5 for 2..4)
如何为 Rails 4 选择多个?
How do you make a select multiple for Rails 4?
推荐答案
class
和 multiple
都是 html_options
的一部分,所以它们应该放在一个散列中.
class
and multiple
are both part of html_options
, so they should go together in a single hash.
改变
<%= f.select :permission, [ ["Read Only", "read"], ["IP Voice Telephony", "ip_voice"], ["IP Video Telephony", "ip_video_telephony"], ["Enterprise Gateways", "enterprise_gateways"], ["Consumer ATAs", "consumer_atas"], ["IP PBX", "ip_pbx"], ["Master of All", "all"] ], {prompt: "Select Permission Level"},
{:multiple => true}, class: "input-lg" %>
到
<%= f.select :permission, [ ["Read Only", "read"], ["IP Voice Telephony", "ip_voice"], ["IP Video Telephony", "ip_video_telephony"], ["Enterprise Gateways", "enterprise_gateways"], ["Consumer ATAs", "consumer_atas"], ["IP PBX", "ip_pbx"], ["Master of All", "all"] ], {prompt: "Select Permission Level"},
{:multiple => true, class: "input-lg"} %>
现在您分别传递它们.所以,select
方法的参数计数变成了 5,而它应该是 4.因此,错误.
Right now you are passing them separately. So, the argument count for select
method is becoming 5 when it should be 4. Hence, the error.
这篇关于Ruby on Rails 4 选择多个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!