2013-05-18 15 views
1

我有一個多標籤模式的選擇標籤,並希望我的控制器更新操作將多個條目保存到has_many_and_belongs_to_many關聯表中。我的控制器應該如何更新動作,我想我必須循環傳入參數?Rails:將多選標籤保存爲has_many_and_belongs_to_many關聯

電流控制器代碼:

def update 
    if @user.update_attributes(params[:user]) 
     flash[:success] = 'Your profile has been updated' 
    end 
end 

回答

2

我不知道這個答案,但我會給一個嘗試。

你的控制器已經足夠好了,我想你在別處設置@user

你需要做的是:

正確命名您的選擇

或用助手正確命名

= form_for @user do |f| 
    = f.collection_select :association_ids, Association.all, :id, :name, nil, multiple: "" 

允許訪問模型中的寫association_ids產生它

class User < ActiveRecord::Base 
    attr_accessible :association_ids 
    ... 
end 
+1

走路軌!謝謝。最後使用這一行:= f.collection_select:association_ids,Association.all,:id,:name,{:selected => @ user.association_ids},{multiple:true} – christian