Rails 3 - 如何在 IRB 控制台中访问 Devise 的 current_user? [英] Rails 3 - How can you get access to Devise's current_user in the IRB console?
问题描述
我正在 IRB 中进行一些设计/调试,需要登录用户,然后才能在我的工作中使用 current_user.
I'm doing some design/debugging in IRB and need to login a user and then be able to use current_user in my efforts.
来自 Brian Deterling 的 答案 另一个问题,我已经能够成功登录并访问具有以下序列的页面响应:
From Brian Deterling's answer to another question, I have been able to successfully login and access a page response with this sequence:
>> ApplicationController.allow_forgery_protection = false
>> app.post('/sign_in', {"user"=>{"login"=>"some-login-id", "password"=>"some-password"}})
>> app.get '/some_other_path_that_only_works_if_logged_in'
>> pp app.response.body
注意:如果您收到 200 响应,则说明您未登录.您需要 302 重定向以指示成功登录.请参阅蒂姆桑特福德的回答.
NOTE: If you get a 200 response you are not logged in. You need a 302 redirect to indicate a successful login. See Tim Santeford's answer.
我已经能够获得会话信息:
I've been able to get session info:
1.9.3-p125 :009 > app.session
=> {"_csrf_token"=>"1yAn0jI4VWzUH84PNTH0lVhjpY98e9echQGS4=", "session_id"=>"89984667d30d0fec71f2a5cbb9017e24"}
我已经尝试了所有我能想到的方法来尝试通过 app
和 app.session
访问 current_user
,但没有成功.我如何获得 current_user
?
I've tried everything I can think of to try to get to current_user
via app
and app.session
, but no luck. How can I get current_user
?
推荐答案
current_user
是控制器的一个属性,所以在 app.post('/sign_in', ...
code> 您可以在 Rails 控制台中调用 app.controller.current_user
来获取 User 对象
current_user
is a property of the controller so after app.post('/sign_in', ...
you can call app.controller.current_user
in your rails console to get the User object
这篇关于Rails 3 - 如何在 IRB 控制台中访问 Devise 的 current_user?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!