【问题标题】:ActionController::InvalidAuthenticityToken in Rails EngineRails 引擎中的 ActionController::InvalidAuthenticityToken
【发布时间】:2015-01-10 16:47:22
【问题描述】:

我正在 Rails 中构建一个 saas 引擎。我在 account#create 方法上遇到了以下错误(使用了warden gem):

ActionController::InvalidAuthenticityToken in Subscribem::AccountsController#create
ActionController::InvalidAuthenticityToken

Rails.root: /home/jma/Documents/subscribem/spec/dummy

Application Trace | Framework Trace | Full Trace
Request

Parameters:

{"utf8"=>"✓",
 "authenticity_token"=>"x5sWQF8eRjwD/fcbdI+MJ1Y1gg7u2x7QvCoN3h1/1UM=",
 "account"=>{"name"=>"test",
 "subdomain"=>"test",
 "owner_attributes"=>{"email"=>"jan@ma.de",
 "password"=>"[FILTERED]",
 "password_confirmation"=>"[FILTERED]"}},
 "commit"=>"Create Account"}

我的应用程序控制器如下所示:

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
    protect_from_forgery with: :exception
end

当将 protect_from_forgery with: :exception 更改为 :null_session 时,错误消失了,但我认为这不是解决此问题的正确方法,因为用户应该在此注册操作后登录,而 :null_session 是仅适用于 API。

该网站也没有为正在注册的用户创建任何似乎不正确的 cookie。

有什么想法吗?

【问题讨论】:

    标签: ruby-on-rails authenticity-token protect-from-forgery


    【解决方案1】:

    我看到您正在关注 Ryan Bigg 关于使用 Rails 构建多租户应用程序的书。

    鉴于此,问题很可能是您需要为您的虚拟应用适当地设置会话 cookie 域(默认情况下它将设置为 example.com)。

    因此,如果您在 subscribem.dev 运行虚拟应用程序(例如通过 Pow),那么您将在 spec/dummy/config/initializers/session_store.rb 中设置会话 cookie 域,如下所示:

    Dummy::Application.config.session_store :cookie_store, key: '_dummy_session', domain: "subscribem.dev"
    

    【讨论】:

      猜你喜欢
      • 2018-11-02
      • 2018-11-27
      • 2017-11-13
      • 2015-05-21
      • 2015-11-23
      • 2014-05-11
      • 2016-11-14
      • 2019-12-25
      • 1970-01-01
      相关资源
      最近更新 更多