繁体   English   中英

Rails 5 +设计:ActionController :: InvalidAuthenticityToken

[英]Rails 5 + Devise: ActionController::InvalidAuthenticityToken

我使用Rails 5和Devise创建了一个新应用程序,设置了身份验证,并且一切正常运行了几个星期。

今天,我尝试登录生产服务器并收到此错误:

ActionController::InvalidAuthenticityToken: ActionController::InvalidAuthenticityToken

经过一番谷歌搜索,我发现我需要进行此更改:

class ApplicationController < ActionController::Base
  #protect_from_forgery with: :exception # because of Devise + Rails 5 behavior
  protect_from_forgery prepend: true  

  def after_sign_in_path_for(resource_or_scope)
    my_listings_path
  end

  def after_sign_out_path_for(resource_or_scope)
    root_path
  end  
end

进行此更改并将其部署在生产服务器上后,我尝试登录-和错误。 但是不同的是-现在应用程序将我重定向到my_listings_path (这是正确的),但是问题是我遇到了以下错误:

NoMethodError: undefined method `listings' for nil:NilClass

所以我看这里是什么问题,并且:

@listings = current_user.listings.order('id DESC')

这意味着current_user为空( nil )-为什么呢? 另外,我在网站的<head>标记中具有以下内容:

 <%= csrf_meta_tags %>

另一个注意事项-在localhost上,一切正常,但是在生产服务器上,我一直在收到这些错误消息。

有什么建议吗?

谢谢!

您需要设置before_action :authenticate_user! 如果希望current_user工作,请在控制器中输入。 我猜测它为您在localhost上运行的原因是因为您在会话中以某种方式设置了current_user。 如果您尝试使用隐身模式,我认为它不起作用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM