【发布时间】:2017-08-06 18:21:47
【问题描述】:
我使用 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 上,一切正常,但在生产服务器上,我不断收到这些错误消息。
有什么建议吗?
谢谢!
【问题讨论】:
标签: ruby-on-rails ruby devise