CanCan 独立角色模型
我一直在遵循此有关独立角色模型实现的指南< a href="https://github.com/ryanb/cancan" rel="nofollow">CanCan。当用户
尝试注册时,在创建分配
时会引发此错误。
User(#21477600) 预期,得到 Symbol(#5785720)
以下 before_save
函数
class User < ActiveRecord::Base
.
.
.
def create_profile
profile = Profile.new :user_id => :id
end
def create_role
Assignment.new :user => :id, :role => Role.find_by_role("user").id
end
end
我正在使用 Devise 生成的 User
以及我想要的 默认用户的角色为“user”,但我显然做错了什么。这应该如何实施?
I've been following this guide on the Separate Role Model implementation in CanCan. When a User
, tries to sign up this error is thrown when creating the Assignment
.
User(#21477600) expected, got Symbol(#5785720)
I'm using a Devise generated User
with the following before_save
functions
class User < ActiveRecord::Base
.
.
.
def create_profile
profile = Profile.new :user_id => :id
end
def create_role
Assignment.new :user => :id, :role => Role.find_by_role("user").id
end
end
I want to default the user's role to "user", but I'm obviously doing something wrong. How should this be implemented?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不确定您是否看过这个,但 Ryan Bates 制作了一份精彩的文档,内容涉及:
单独的角色模型编辑
:
这是我目前正在使用的。我相信您的“分配”与我的“用户角色”相同。
user.rb
role.rb
user_role.rb
然后在我的能力中。rb
然后我可以轻松地分配角色,就像在我的种子文件中一样,通过执行以下操作
: [role_name],我本质上是创建一个 UserRole,它有一个 user_id 和一个 role_id。
Not sure if you've seen this or not, but Ryan Bates has produced a wonderful document regarding:
Separate Role Models
EDIT:
Here's what I am currently using. I believe your 'Assignment' is the same as my 'UserRole'.
user.rb
role.rb
user_role.rb
Then in my ability.rb
Then I can easily assign roles, like in my seed file by doing something like:
So by calling user.roles << [role_name], I am essentially creating a UserRole, which has a user_id and a role_id.
可能有一些更有效的方法来实现这一点,但如果没有确切的模型关联,我无法判断。
无论如何,我认为这应该有效:
由于您指定 :user 而不是 :user_id,因此您应该传递 self. :role 也是如此。如果您指定了 :role_id 那么您应该在 find_by_role 之后输入 .id,但由于您只指定 :role 然后删除 .id
There might be some more effective ways to accomplish this, but I cant tell without the exact model associations.
Anyway, I think this should work:
Since you specify :user and not :user_id, you should pass self. The same thing for :role. If you had specified :role_id then you should have entered .id after find_by_role but since you only specify :role then remove .id
看起来您正在将符号传递给需要对象的哈希条件。
丹尼曼的回答应该有效。你也可以这样做
(但在我看来,丹尼的更好)
最后一个建议——为什么不说角色的名字是“名字”,而不是“角色”。那么你就会这样做,Role.find_by_name('user')。这对于后来的程序员来说会更容易遵循。
It looks like you're passing symbols to hash conditions that are expecting objects.
DanneManne's answer should work. You could alternatively do
(but Danne's is better, imo)
One last suggestion -- why not say the name of the role is "name", not "role". So then you'd be doing, Role.find_by_name('user'). That would be easier for a subsequent programmer to follow.
首先,您不应该使用保存回调,因为它会在创建和创建时触发。更新。
其次,如果您在模型之间建立这样的关联:
您将拥有诸如
user.profile
、user.build_profile
和user.create_profile
等方便的方法。构建& create 将自动在个人资料上设置user_id
。您可以在回调中使用它们,而无需定义任何方法。请注意,在保存用户之前,它没有 id。因此,您需要使用
before_create :build_profile
或after_create :create_profile
。第一个将在内存中创建配置文件,该配置文件将在用户保存后自动保存,第二个非常简单。也有类似的分配方法:
user.assignments.build
user.assignments.create
。所以 User 的最终代码看起来像这样Firstly, you should not use save callback because it will be fired on both create & update.
Secondly, if you set up associations between models like that:
You will have convenient methods like
user.profile
,user.build_profile
anduser.create_profile
. Build & create will set upuser_id
on profile automatically. You can use them in your callbacks without having to define any methods.Note that before user is saved it does not have an id. So you need to use either
before_create :build_profile
eitherafter_create :create_profile
. The first one will create profile in memory that will be autosaved after user is saved, the second one is pretty straightforward.There will be similar methods for assignments too:
user.assignments.build
user.assignments.create
. So the final code for User will look something like this