在模型中访问了Rails/devise current_user,但是它为零 [英] Rails/devise current_user accessed in model but it's nil

查看:102
本文介绍了在模型中访问了Rails/devise current_user,但是它为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在另一个模型中访问 current_user .我在Google上搜索并找到了一些解决方案,并尝试了.

I needed to access the current_user in my another model. I googled and found a few solutions and tried this.

但是只有一个区别:我没有在控制器中设置 User.current_user . 为什么?

But just one difference: I didn't set the User.current_user in the controller. WHY?

就像我遵循的答案一样我不是从视图中向第二个模型添加数据,而是通过url渲染数据(使用open-uri,这是我正在获取的csv数据).
因此,在第二个模型中,我这样做:

Like in the answer I followed I'm not adding data to my second model from views but by rendering data via a url (with open-uri, it's csv data I'm fetching).
So in my second model I do:

Person.create(username: User.current_user.username)  

它给出:

NoMethodError:nil:NilClass的未定义方法用户名"

NoMethodError: undefined method `username' for nil:NilClass

这是行不通的(我想这很明显).在控制台中,当我执行User.current_user时,它显示:

which isn't working (which is obvious I guess). In console when I do User.current_user it shows:

1.9.3p448:002> User.current_user
=>无

1.9.3p448 :002 > User.current_user
=> nil

我认为它为什么不起作用,是因为我直接从模型访问 User.current_user ,除非给出模型,否则模型无法获取current_user. (对吗?)

I think why it isn't working is because I'm accessing the User.current_user directly from model and model cannot get the current_user unless it is given that. (right?)

但是,如果我通过登录页面访问它并在控制器中设置User.current_user,这肯定可以工作.

But this would definitely work if I access it via a login page and set the User.current_user in the controller.

但是,由于我是直接从url获取数据,所以我是直接在模型本身中为Person模型创建新条目.

那我该如何设置User.current_user?

So how do I set the User.current_user?

有什么解决方法吗?必须对该问题的标题进行编辑.

Is there any workaround for this? Edit's for the question's title are required.

推荐答案

current_user默认情况下可作为Devise中的帮助方法使用.来自文档:

current_user is available by default as a helper method within Devise. From the documentation:

# For the current signed-in user, this helper is available:
current_user

current_user本身不是直接通过模型访问的,而是通过Devise模块进行的,该模块查找登录到当前会话中的User对象,然后返回到current_user

The current_user isn't accessed directly via the model, per se, but rather, though the Devise module, which looks up the User object that is logged into the current session, and then returns to current_user.

因此,无法通过User.current_user访问当前用户(错误消息说的是User上没有current_user方法).您完全可以通过在控制器或视图中调用current_user辅助方法来访问它.

Thus, the current user isn't accessed via User.current_user (there is no current_user method on User, as the error message is saying). You access it purely by invoking the current_user helper method within a controller or view.

更新:

建议您将控制器和模型层分开.提出建议的一种方法是在Person模型上创建一个类函数,其中您从控制器内部显式传递了User对象的username:

You're well advised to keep your controller and model layers separate. One way of doing what you've proposed is to create a class function on the Person model wherein you explicitly pass the username of your User object from within your controller:

# in your controller
Person.create_with_username(:username => current_user.username)

# app/models/person.rb
def self.create_with_username(username)
    self.create(username)
end

这篇关于在模型中访问了Rails/devise current_user,但是它为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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