Ruby on Rails 4 select multiple

匿名 (未验证) 提交于 2019-12-03 02:00:02

问题:

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} :

  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?

回答1:

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

Change

 true}, class: "input-lg" %> 

To

 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.



回答2:

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

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

should be

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


回答3:

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:

 

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



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!