如何从 Rails 控制台使用 Devise 登录用户? [英] How to sign in a user using Devise from a Rails console?

查看:31
本文介绍了如何从 Rails 控制台使用 Devise 登录用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

加载 Rails 控制台后,我应该如何登录用户?

After loading the Rails console, how should I sign in a user?

Devise 提供了一个可以在测试中使用的测试助手,我已经尝试在控制台中使用:

Devise provides a test helper which can be used in tests and I've tried to use in console:

>> include Devise::TestHelpers
>> helper.sign_in(User.first)

但我明白了:

NoMethodError: undefined method `env' for nil:NilClass

无论如何,我想使用真正的设计助手而不是这个测试助手.有什么办法可以做到这一点吗?

Anyway, I would like to use the real devise helper and not this test helper. Is there any way to achieve this?

推荐答案

这是我能够做到的一种方法:

Here's one way I was able to do it:

>> ApplicationController.allow_forgery_protection = false
>> app.post('/sign_in', {"user"=>{"login"=>"login", "password"=>"password"}})

然后你可以这样做:

 >> app.get '/some_other_path_that_only_works_if_logged_in'
 >> pp app.response.body

这篇关于如何从 Rails 控制台使用 Devise 登录用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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