在 rails 3 中,current_user 是否有可能是初始化程序? [英] is it possible for current_user to be initializer, in rails 3?

查看:34
本文介绍了在 rails 3 中,current_user 是否有可能是初始化程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在初始化程序中使用诸如 current_userUser.current 之类的东西?我试过类似

Is it possible to use something like current_user or User.current in the initializers? I've tried something like

Something = Something::Some.new configure do |something|

    something.somethingelse = current_user.key

end

但它指出它找不到 key 但我知道 key 是 User 表中的一列.我可以在模型或控制器下执行 something = current_user.keysomething = User.key 并且它工作正常.

but it states it can't find the key but I know that key is a column in User table. I can do something = current_user.key or something = User.key in under model or controller and it works fine.

推荐答案

current_user 通常设置在应用程序的 application_controller 中.例如,如果您使用像 Devise 这样的 gem 来处理用户身份验证,他们会为您设置此类方法.

current_user is usually set in the application_controller of your application. If you use a gem like Devise to handle user authentications for example, they take care of setting such method for you.

初始化程序的代码在您在服务器(本地计算机或远程服务器)上启动应用程序时执行,因此您了解current_user"(理解登录"用户)根本不存在(还).

The initializers' code is executed when you launch your application on your server (local machine or remote server), therefor you understand that a "current_user" (understand a "logged in" user) simply does not exists (yet).

这篇关于在 rails 3 中,current_user 是否有可能是初始化程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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