问题
I have built a small ruby webservice, in this I have implemented cancan authorization.
I followed this tutorial. The problem is that, I can't find out the way to assign at the user, when they do the registration to my site, the base role level.
I find out to do this with a checkbox, but it's not what I want. My idea was to put this assignment directly into the registrations_controller, but I failed to save the role.
I hope that somebody can help me.
Thank you.
回答1:
This is what worked for me
user.rb:
after_create :default_role
private
def default_role
self.roles << Role.where(:name => 'User').first
end
回答2:
I had the same problem, but I am using embedded association from rbates: http://railscasts.com/episodes/189-embedded-association
user.rb:
before_create :default_role
private
def default_role
self.roles = ['client']
end
Works like a charm, but pay attention that the hook is before_create, not after_create, because the before_create runs just before the insert operation.
The after_create is after the insert operation, which in my case is late.
回答3:
I have rebuild the migration, I have unified the user and role tables, so now I can assign all without problem.
Thank you.
来源:https://stackoverflow.com/questions/6052504/ruby-on-rails-cancan-and-default-role-assignment