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