Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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" %>

My controller, I added :permission => [] :

def user_params
  params.require(:user).permit(:name, :email, :password, :password_confirmation, :admin, :permission => [])
end

The error I get for my view, f.select:

wrong number of arguments (5 for 2..4)

How do you make a select multiple for Rails 4?

like image 695
DDDD Avatar asked Apr 23 '14 19:04

DDDD


1 Answers

class and multiple are both part of html_options, so they should go together in a single hash.

Change

<%= 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" %>

To

<%= 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"} %>

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.

like image 80
Kirti Thorat Avatar answered Nov 03 '22 00:11

Kirti Thorat