2014-04-23 173 views
9

我有一個創建新用戶的表單。我正在嘗試添加一個下拉選項來選擇權限級別。我希望能夠爲每個用戶選擇多個權限級別。Ruby on Rails 4選擇多個

這是我的看法,我說{: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 => []

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

爲我的觀點錯誤,f.select:

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

你如何爲Rails 4選擇多個?

回答

13

classmultiple都是html_options的一部分,所以它們應該放在一個散列中。

變化

<%= 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.因此,錯誤。

+0

+1解釋,哇,你能不能請您給一個參考,我有一個很難找到一個。 – DDDD

+1

您可以查看http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-select和http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/select –

+0

那麼,我以爲我是第一個。刷新頁面。 :D –

5

您的選擇:類不在哈希html_options:

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

應該

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

+1解釋,我必須錯過該部分:/ – DDDD

+1

這裏是API:http:// apidock。com/rails/ActionView/Helpers/FormBuilder/select –

+0

當您在表單構建器中使用助手時,它會有點令人困惑,因爲它們比非包裝版本少一個選項,因爲表單構建器本身提供了該對象。 –

4

我沒有測試它,但迄今爲止該錯誤信息是非常直接的,你試圖使用#select方法使用5個參數,它最多接受4個參數,閱讀API似乎你應該提供'class'屬性在你提供'multiple'的同一個散列中,因爲它們都是html_options 。

嘗試使用這樣的:

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

這也將是很好的有權限去耦陣列到另一個地方。也許這將有助於維持。

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

+1

+1令人敬畏的信息:D – DDDD