Rails 中的 method_missing 和 Association_proxy
所以,这是我的问题。我目前正在为 Rails 站点构建一个简单的身份验证系统。我为此设置了 3 个类:Person、Session 和 Role。在我的 Person 模型中,我定义了 method_missing
来根据 本指南。
在我的 application_controller 中,我有一些逻辑来处理登录和注销,其结果通过以下方式为我提供当前登录的用户: @user = @application_session.person
其中 @application_session
是当前会话
现在在我的一个控制器中,我不希望任何人能够执行任何操作,除非他们是管理员,所以我包括: before_filter @user.is_an_admin?
这会引发 NoMethodError
,即使我在模型中定义了 method_missing
。我尝试定义 is_an_admin?
,让它始终返回 true 作为测试,这很有效。
根据 这个问题,我认为问题可能与代理有关协会。当我跑步时: 放置@user.proxy_owner
我得到一个会话对象,因为每个用户(Person)可以有许多会话,并且我从当前会话中获取了我的用户(Person)。
我很困惑为什么 @user.is_an_admin?
没有调用我的 Person 控制器中的 method_missing 方法。如果您需要更多信息或代码片段,请告诉我。
我在 Ruby 1.9 上使用 Rails 3
So, here's my problem. I currently am building a simple authentication system for a rails site. I have 3 classes for this: Person, Session, and Role. In my Person model I have defined method_missing
to dynamically capture roles according to this guide.
In my application_controller I have some logic to deal with logins and log-outs, the result of which gives me the currently logged in user via:@user = @application_session.person
Where @application_session
is the current session
Now in one of my controllers, I don't want anyone to be able to do anything unless they are an admin, so I included:before_filter @user.is_an_admin?
This raises a NoMethodError
, even though I have method_missing
defined in my model. I tried defining is_an_admin?
, having it always return true as a test, and that works.
According to this question, I think the problem might have something to do with proxy associations. When I run:puts @user.proxy_owner
I get a session object, since each user (Person) can have many sessions, and I got my user (Person) from the current session.
I am very confused why @user.is_an_admin?
is not calling the method_missing method in my Person controller. Please let me know if you need more information or code snippets.
I am using Rails 3 on Ruby 1.9
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为 method_missing 对于这样的任务来说是多余的。
现在,如果您有属于 User 的 Session 类,那么您可以这样:
PS Check cancan,也许它会更好地满足您的需求。
I'd consider a method_missing an overkill for such task.
Now, if you have Session class, which belongs_to User, then you can have this:
P.S. Check cancan, perhaps it'll suit your needs better.
我在系统中使用类似的权限检查来检查 User >角色>权限关联:
User.current_user.can_sysadmin?
在我的控制器中,我必须使用:
User.current_user.send('can_sysadmin?')
这可能也适合您。
I use a similar permissions check in my system to check the User > Role > Permissions association:
User.current_user.can_sysadmin?
In my controllers I have to instead use:
User.current_user.send('can_sysadmin?')
This may work for you as well.
我通过将
method_missing
方法移动到我的application_controller.rb
解决了这个问题。我稍微改变了方法的逻辑来检查用户,如果找到,则动态检查角色。如果事情不合规矩,我会将方法重定向到 root_url ,或者在用户与请求的角色匹配时返回 true。最后,在我的报告控制器中,我使用了
before_filter :is_an_admin?
并得到了我想要的结果。但是,我仍然不清楚为什么method_missing
必须在我的应用程序控制器中定义,而不是直接在 Person (又名@user
)模型中定义?I have solved this by moving the
method_missing
method to myapplication_controller.rb
. I change the logic of the method a little to check for a user, and if found, dynamically check the role. If things were not kosher, I had the method redirect toroot_url
or return true if the user matched the requested roles.Finally, in my reports controller, I used
before_filter :is_an_admin?
and got my desired results. However, I am still unclear as to whymethod_missing
had to be defined in my application controller as opposed to directly in the Person (aka@user
) model?