2014-10-06 66 views
1

零以下是完整的错误:设计CURRENT_USER =在ApplicationController中

Can't verify CSRF token authenticity 
    User Load (0.3ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 5 ORDER BY `users`.`id` ASC LIMIT 1 
    (0.1ms) BEGIN 
    (0.2ms) COMMIT 
Completed 500 Internal Server Error in 22ms 

NoMethodError - undefined method `has_role?' for nil:NilClass: 
    app/controllers/application_controller.rb:8:in `authenticate_admin_user!' 

我可以确认我是在用管理用户的时间登录。

这里是触发错误的控制器:

class ApplicationController < ActionController::Base 
    def authenticate_admin_user! 
    unless current_user.has_role? :admin 
     flash[:alert] = "This area is restricted to administrators only." 
     redirect_to main_app.root_path 
    end 
    end 
+0

你发送给您的应用程序,以及如何什么要求?看起来你错过了一个CSRF令牌。 – 2014-10-06 07:20:53

回答

1

在你的应用程序控制器添加此行,在CURRENT_USER方法将被定义

before_action :authenticate_user! 
+0

是的,修正了这个问题..但不幸的是现在我看到无法验证CSRF令牌的真实性 – Abram 2014-10-06 03:41:00

+0

对不起,但我没有面对这个问题之前,你可以检查这[问题](http://stackoverflow.com/questions/19861402/devise-user-sign-in-gives-authentication-error-for-csrf-token-authenticity-token)它可能帮帮我。 – 2014-10-06 03:50:56

0

变化
unless current_user.has_role? :admin

要这样:
unless current_user.try(:has_role?, :admin)

,做它是什么,它会try调用:has_role?方法(使用:admin参数,而不是raisi它会返回一个异常nil

+0

但问题是,current_user应该定义 – Abram 2014-10-06 03:08:36

+0

抱歉,但我没有面对这个问题之前,看到这个[问题](http://stackoverflow.com/questions/19861402/devise-user-sign-in-gives -authentication-error-for-csrf-token-authenticity-token),它可能有帮助 – 2014-10-06 03:46:58

相关问题