2012-01-30 64 views
2

我試圖創建使用形式爲我的用戶的用戶角色,設置的has_many:通過與在軌道上的表單3

<%= form_for @user do |f| %> 
    <%= f.error_messages %> 
    <p> 
    <%= f.label :username %><br /> 
    <%= f.text_field :username %> 
    </p> 
    <p> 
    <%= f.label :email, "Email Address" %><br /> 
    <%= f.text_field :email %> 
    </p> 
    <p> 
    <%= f.label :password %><br /> 
    <%= f.password_field :password %> 
    </p> 
    <p> 
    <%= f.label :password_confirmation, "Confirm Password" %><br /> 
    <%= f.password_field :password_confirmation %> 
    </p> 

    <% # f.select :roles, Role.all.map {|r| [r.title]} %> 

    <% Role.all.each do |role| %> 
    <div> 
     <%= check_box_tag :role_ids, role.id, @user.roles.include?(role), :name => 'user[role_ids][]' -%> 
     <%= label_tag :role_ids, role.title -%> 
    </div> 
    <% end -%> 

    <p><%= f.submit (@user.new_record? ? "Sign up" : "Update"), :id => :sign_up %></p> 
<% end %> 

這是我在我的模型協會

class user < ActiveRecord::Base 
    has_many :assignments 
    has_many :roles, :through => :assignments 
end 

class assignment < ActiveRecord::Base 
    belongs_to :user 
    belongs_to :role 
end 

class role < ActiveRecord::Base 
    has_many :assignments 
    has_many :users, :through => :assignments 
end 

通過使用我在開始介紹的表單創建用戶和角色之間的分配的方式是什麼?

我在我的用戶控制器創建操作是這樣的:

def create 
    @user = User.new(params[:user]) 
    if @user.save 
     session[:user_id] = @user.id 
     render :action => 'dashboard' 
    else 
     render :action => 'new' 
    end 
    end 

當我把我的形式,我得到的錯誤:

的ActiveRecord :: AssociationTypeMismatch在UsersController#創建

角色(# 70331681817580)預計得到String(#70331650003400)

Request 

Parameters: 

{"utf8"=>"✓", 
"authenticity_token"=>"WHpOW+DmymZ2pWmY9NHSuodf2vjyKdgMNZcc8NvCNa0=", 
"user"=>{"username"=>"ioio", 
"email"=>"[email protected]", 
"password"=>"[FILTERED]", 
"password_confirmation"=>"[FILTERED]", 
"roles"=>["2"]}, #### <<< ==== This is the checkbox that I selected for this request. 
"commit"=>"Sign up"} 

歡迎任何幫助。

+0

運氣好的解決方案?該修補程序適用於我,所以我很好奇它是否也解決了您的問題。 – miked 2012-02-02 19:21:07

回答

4

使用當前的形式和依據:

<%= check_box_tag :role_ids, role.id, @user.roles.include?(role), :name => 'user[role_ids][]' -%> 

在提交時,您的PARAMS應該像(注意 'role_ids' 而不是 '角色'):

Request 

Parameters: 

{"utf8"=>"✓", 
"authenticity_token"=>"WHpOW+DmymZ2pWmY9NHSuodf2vjyKdgMNZcc8NvCNa0=", 
"user"=>{"username"=>"ioio", 
"email"=>"[email protected]", 
"password"=>"[FILTERED]", 
"password_confirmation"=>"[FILTERED]", 
"role_ids"=>["2"]}, #### <<< ==== This is the checkbox that I selected for this request. 
"commit"=>"Sign up"} 

如果是這樣的情況下,您將不得不實例化您的角色並將它們設置爲控制器中的用戶:

def create 
    @user = User.new(params[:user]) 
     roles = Role.find(params[:user][:role_ids]) rescue [] 
     @user.roles = roles 
    if @user.save 
      session[:user_id] = @user.id 
      render :action => 'dashboard' 
    else 
      render :action => 'new' 
    end 
  end 

... and simil arly:

def update 
    @user = User.where(:username=>params[:id]).first 
    roles = Role.find(params[:user][:role_ids]) rescue [] 
    @user.roles = roles 
    if @user.update_attributes(params[:user]) 
     redirect_to users_url, :notice => "Successfully updated user." 
    else 
     render :action => 'edit' 
    end 
    end 
0

錯誤消息給出了提示:爲了能夠保存用戶對象,您首先需要以某種方式創建關聯的角色對象。目前,您只有一個字符串數組是Role ID。

您需要在用戶模型上使用accepts_nested_attributes_for方法。

+0

我試圖在用戶模型(user.rb)中添加accep_nested_attributes_for:roles,但沒有運氣。 – Goles 2012-01-30 22:12:16

+0

@ Mr.Gando看@miked答案 - 他正確地指出你需要'role_ids',而不是'roles'作爲params散列(和複選框)中的鍵。然後,可以通過在用戶模型上使用'accep_nested_attributes_for'來進一步簡化代碼。 – maprihoda 2012-01-31 10:06:29