current_user使用Devise在控制器中返回TrueClass而不是用户 [英] current_user returns TrueClass instead of a User in a controller using Devise
问题描述
上下文当current_user是管理员做
应该渲染做
sign_in工厂(:admin)
get:index
assert_response:success
end
end
在控制器本身中,有以下的before_filter:
def redirect_if_not_admin
将current_user
redirect_to root_path,除非current_user.try(:admin?)
end
这是我测试的输出:
true
false
NoMethodError :未定义的方法`admin?'为true:TrueClass
所以current_user在我使用sign_in测试。但是当我没有,current_user为零,所以它肯定会受到影响。感谢您提前获得任何帮助。
我用了确认!方法在after_create / build中的资源上,而不是在工厂中设置属性,并且这样做。当网站上有未确认的用户时,current_user等于true(已注册但尚未点击发送给他们的确认链接的用户)。
I'm using Devise 1.0.8 on a Rails 2.3.8 project. In my controller test I have:
context "when current_user is admin" do
should "render" do
sign_in Factory(:admin)
get :index
assert_response :success
end
end
And in the controller itself, there's the following before_filter:
def redirect_if_not_admin
puts current_user
redirect_to root_path unless current_user.try(:admin?)
end
This is the output from my test:
true
false
NoMethodError: undefined method `admin?' for true:TrueClass
So current_user is true when I use sign_in in the test. However when I don't, current_user is nil, so it's definitely being affected. Thanks in advance for any help.
I used the confirm! method on the resource in an after_create/build instead of setting the attributes in the factory and that did the trick. current_user equals true when there is an unconfirmed user on the site (someone who has signed up but has yet to click on the confirmation link sent to them).
这篇关于current_user使用Devise在控制器中返回TrueClass而不是用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!