Ruby on Rails、cancan 和默认角色分配
我构建了一个小型 ruby web 服务,在其中我实现了 cancan 授权。
我遵循了本教程。问题是,当用户注册到我的网站时,我无法找到分配基本角色级别的方法。
我发现用复选框来做到这一点,但这不是我想要的。我的想法是直接将这个分配放入registrations_controller中,但我未能保存该角色。
我希望有人能帮助我。
谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这对我有用
user.rb:
This is what worked for me
user.rb:
我遇到了同样的问题,但我正在使用 rbates 的嵌入式关联:
http://railscasts.com/episodes/189-embedded-association
用户.rb:
before_create :default_role
就像一个超级按钮一样工作,但要注意钩子是before_create,而不是after_create,因为 before_create 在插入操作之前运行。
after_create 是在插入操作之后,在我的例子中已经晚了。
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
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.
我已经重建了迁移,我已经统一了用户和角色表,所以现在我可以毫无问题地分配所有内容。
谢谢。
I have rebuild the migration, I have unified the user and role tables, so now I can assign all without problem.
Thank you.