2011-10-20 15 views
0

私のデータベースで、ユーザーとロールの間のアソシエーションが作成されていないことがわかります。ここでRailsのsimple_formアソシエーションは、ユーザーアカウント登録時に作成されません

は私のフォームです:

<div style="margin-left:40px;float:left;width:620px;"> 
<h2>Create User</h2> 
<%= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %> 
<%= f.input :username, :label => 'Brugernavn' %> 
<%= f.input :email, :label => 'E-mail' %> 
<%= f.input :password, :label => 'Kodeord' %> 
<%= f.input :password_confirmation, :label => 'Bekræft kodeord' %> 
<%= f.association :roles, :label => 'Role', :as => :check_boxes, :collection => ['admin', 'user', 'manager'] %> 
<%= f.submit 'Opret bruger' %> 
<% end %> 

私のユーザモデル:

class User < ActiveRecord::Base 
has_and_belongs_to_many :roles 
make_voter 
has_many :posts 
    # Include default devise modules. Others available are: 
    # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable 
    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable 
validates_presence_of :username, :password, :password_confirmation, :email 
validates_uniqueness_of :username, :email 
    # Setup accessible (or protected) attributes for your model 
    attr_accessible :email, :password, :password_confirmation, :remember_me, :username 
end 

私のロールモデル:

class Role < ActiveRecord::Base 
has_and_belongs_to_many :users 
end 

私が参加し、テーブルroles_usersを作成しました。

フォームはのparamsを提出:

Started POST "/users" for 127.0.0.1 at 2011-10-20 13:10:06 +0200 
    Processing by Devise::RegistrationsController#create as HTML 
    Parameters: {"utf8"=>"Ô£ô", "authenticity_token"=>"4tDVvIWOGBEmQpk/Vr9k8huIdB1 
ghXxNGEcJDj+uuZk=", "user"=>{"username"=>"sometest", "email"=>"[email protected] 
mail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "rol 
e_ids"=>["", "", "user", ""]}, "commit"=>"Opret bruger"} 
    ←[1m←[35mSQL (7.0ms)←[0m describe `roles_users` 
WARNING: Can't mass-assign protected attributes: role_ids 
    ←[1m←[36mSQL (1.0ms)←[0m ←[1mBEGIN←[0m 
    ←[1m←[35mSQL (2.0ms)←[0m SELECT 1 FROM `users` WHERE (`users`.`email` = BINAR 
Y '[email protected]') LIMIT 1 
    ←[1m←[36mSQL (1.0ms)←[0m ←[1mSELECT 1 FROM `users` WHERE (`users`.`username` 
= BINARY 'sometest') LIMIT 1←[0m 
    ←[1m←[35mCACHE (0.0ms)←[0m SELECT 1 FROM `users` WHERE (`users`.`email` = BIN 
ARY '[email protected]') LIMIT 1 
    ←[1m←[36mSQL (6.0ms)←[0m ←[1mdescribe `users`←[0m 
    ←[1m←[35mAREL (0.0ms)←[0m INSERT INTO `users` (`email`, `encrypted_password`, 
`reset_password_token`, `reset_password_sent_at`, `remember_created_at`, `sign_ 
in_count`, `current_sign_in_at`, `last_sign_in_at`, `current_sign_in_ip`, `last_ 
sign_in_ip`, `created_at`, `updated_at`, `username`) VALUES ('[email protected] 
ail.com', '$2a$10$g.c.4kawWiEPDalKDwxqr.ZvvYx769t2arVKUcQh9kcv9Jf5tdFoe', NULL, 
NULL, NULL, 0, NULL, NULL, NULL, NULL, '2011-10-20 11:10:06', '2011-10-20 11:10: 
06', 'sometest') 
    ←[1m←[36mSQL (54.0ms)←[0m ←[1mCOMMIT←[0m 
    ←[1m←[35mSQL (1.0ms)←[0m BEGIN 
    ←[1m←[36mAREL (1.0ms)←[0m ←[1mUPDATE `users` SET `last_sign_in_at` = '2011-10 
-20 11:10:06', `current_sign_in_at` = '2011-10-20 11:10:06', `last_sign_in_ip` = 
'127.0.0.1', `current_sign_in_ip` = '127.0.0.1', `sign_in_count` = 1, `updated_ 
at` = '2011-10-20 11:10:06' WHERE `users`.`id` = 9←[0m 
    ←[1m←[35mSQL (1.0ms)←[0m COMMIT 
Redirected to http://localhost:3000/ 
Completed 302 Found in 287ms 

答えて

0

はあなたUserモデルへ

accepts_nested_attributes_for :roles 

を追加します。 許可されていないすべての属性をブロックするので、attr_accessibleも参照してください。そこに:rolesを追加する必要があるかもしれません。

+0

attr_accessibleにroles_idsを追加しました –

関連する問題