2012-01-30 4 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 ::がUserController#でAssociationTypeMismatchは(

役割を作成する# 70331681817580)expected、got 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

答えて

4

あなたの現在のフォームを使用してに基づいて:これがあれば

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

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

に提出し、あなたのparamsは( 'role_ids' といないの役割」を注意してください)のようになります。場合、あなたの役割をインスタンス化し、コントローラのユーザのために設定する必要があります:

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 

...とsimil場所:

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

エラーメッセージには、ヒントが表示されます。ユーザーオブジェクトを保存できるようにするには、まず関連付けられたロールオブジェクトを何らかの方法で作成する必要があります。現時点では、役割IDである文字列の配列しかありません。

ユーザーモデルでaccepts_nested_attributes_forメソッドを使用する必要があります。

+0

Userモデル(user.rb)でaccepts_nested_attributes_for:rolesを追加しようとしましたが、運がありません。 – Goles

+0

@ Mr.Gando @mikedの答えを見てください。彼は、あなたが 'role_ids'を必要とし、paramsのハッシュ(およびチェックボックス)のキーとして' role'を必要としていないことを正しく指摘しています。次に、ユーザモデルで 'accepts_nested_attributes_for'を使ってコードをさらに単純化することができます。 – maprihoda

関連する問題