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

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

问题描述

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



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

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

但我得到:

  NoMethodError:undefined method`env'for nil:NilClass 

反正,我想使用真正的devise助手,而不是这个测试助手。有没有办法实现这一点?

解决方案

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

 >> 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


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

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)

But I get:

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"}})

Then you can do:

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

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

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