9

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?

3 Answers 3

14

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.

Sign up to request clarification or add additional context in comments.

5 Comments

+1 Explanation, Wow, can you please give a reference, I was having a hard time finding one.
Well, I thought I was first. Refresh your page. :D
Actually @KirtiThorat beat me by a few seconds so please accept her answer.
Kirti, how will the multiple option version be saved in the db? I can't find a good read on it.
6

Your option for :class is not in the hash for html_options:

{:multiple => true}, class: "input-lg"

should be

{:multiple => true, class: "input-lg"}

1 Comment

When you use the helpers in a form builder it's a little confusing because they take one fewer option than the non-wrapped versions, since the form builder itself gives the object.
4

I didn't test it so far but the error message is pretty straight forward, you are trying to use the #select method using 5 params and it accepts at most 4 params, reading the API it seems that you should provide the 'class' attribute in the same hash you have provided the 'multiple' as they both are html_options.

Try to use it like this:

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

It would also be good to have that permissions array decoupled to another place. Perhaps it will help to maintain.

http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-select

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.