轨道3色器件,CURRENT_USER是不是在一个模型访问? [英] Rails 3 devise, current_user is not accessible in a Model?

查看:260
本文介绍了轨道3色器件,CURRENT_USER是不是在一个模型访问?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我project.rb模型中,我试图创建范围与一个动态的变量:

in my project.rb model, I'm trying to create a scope with a dynamic variable:

scope :instanceprojects, lambda { 
    where("projects.instance_id = ?", current_user.instance_id)
} 

我收到以下错误:未定义的局部变量或方法`CURRENT_USER'为#

I get the following error: "undefined local variable or method `current_user' for #"

凡在控制器我可以访问current_user.instance_id ...还有一个原因是该模型不能访问它,一种方式来获得访问权限?此外,这是正确的地方,以创建像上面的一个范围,或不属于控制器?

Where in the controller I can access current_user.instance_id... Is there a reason the model can't access it and a way to get access? Also, is this the right place to create a scope like the above, or does that belong in the controller?

感谢

推荐答案

这没有多大意义,因为你已经指出。该CURRENT_USER不属于逻辑,所有的模型,应该在控制层面处理。

This doesn't make much sense, as you already pointed. The current_user doesn't belong to model logic at all, it should be handled on the controller level.

但你仍然可以创建范围那样,只是参数从控制器传递给它:

But you can still create scope like that, just pass the parameter to it from the controller:

scope :instanceprojects, lambda { |user|
    where("projects.instance_id = ?", user.instance_id)
} 

现在,你可以在控制器中调用它:

Now you can call it in the controller:

Model.instanceprojects(current_user)

这篇关于轨道3色器件,CURRENT_USER是不是在一个模型访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆