Hopefully someone can help me on this one, it's absolutely driving me crazy!
Basically, I have created an Authentication system on my app, using Devise and CanCan, which works fine. The problem arises when I try to assign a Role to a User on signup, the Users Role does not save, the sign-up is successful, but the User has no assigned Role. In my Console the Users Role comes up as nil!
I've set up a HABTM association between Users and Roles, and tried various options. Rails Console returns User Role:nil even if I input the role as a String in the sign-up form.
I am new to Rails and programming in general, would really appreciate some assistance people! Many Thanks in
User Model:
class User < ActiveRecord::Base
has_and_belongs_to_many :roles
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :username, :password, :password_confirmation, :remember_me, :role_ids
def role?(role)
return !!self.roles.find_by_name(role.to_s.camelize)
end
end
Snippet from New Registration View:
<fieldset>
<legend>Sign Up</legend>
<div class="inputs">
<%= f.input :username, :required => true, :autofocus => true %>
<%= f.input :email, :required => false, :autofocus => true %>
<%= f.input :password %>
<%= f.input :password_confirmation, :label => "Confirm Password" %>
</div>
<div>
<%= f.input :roles, :required => true, :collection => Role.find(:all).collect {|c| [ c.name, c.id ]}, :label => "Role" %>
</div>
Params
Processing by Devise::RegistrationsController#create
as HTML Parameters: {"commit"=>"Sign up", "authenticity_token"=>"1HCmfOVATkz/LbALboU+Z2Bg/lBQseVNB2NeAG7GPHc=", "utf8"=>"✓", "user"=>{"password_confirmation"=>"[FILTERED]", "username"=>"test", "role_ids"=>["2"], "password"=>"[FILTERED]", "email"=>"[email protected]"}}
[1m[35mRole Load (0.1ms)[0m SELECT "roles".* FROM "roles" WHERE "roles"."id" = 2 LIMIT 1 [1m[36mSQL (0.1ms)[0m [1mSELECT 1 FROM "users" WHERE ("users"."email" = '[email protected]') LIMIT 1[0m
[1m[35mAREL (0.3ms)[0m INSERT INTO "users" ("reset_password_token", "role", "email", "remember_created_at", "current_sign_in_ip", "encrypted_password", "updated_at", "created_at", "sign_in_count", "username", "last_sign_in_at", "reset_password_sent_at", "last_sign_in_ip", "current_sign_in_at") VALUES (NULL, NULL, '[email protected]', NULL, NULL, '$2a$10$8J6F2NgkK6Tas0AVMiocUujimZ7K3XcYFSmWGeYUzmGDN55WVUHxO', '2011-07-10 23:30:45.820893', '2011-07-10 23:30:45.820893', 0, 'test', NULL, NULL, NULL, NULL)
[1m[36mSQL (0.1ms)[0m [1mINSERT INTO "roles_users" ("role_id", "user_id") VALUES (2, 33)[0m [1m[35mAREL (0.1ms)[0m UPDATE "users" SET "current_sign_in_ip" = '127.0.0.1', "updated_at" = '2011-07-10 23:30:45.827053', "sign_in_count" = 1, "last_sign_in_at" = '2011-07-10 23:30:45.826761', "last_sign_in_ip" = '127.0.0.1', "current_sign_in_at" = '2011-07-10 23:30:45.826761' WHERE "users"."id" = 33Redirected to http://localhost:3000/ Completed 302 Found in 163ms