在此之前,我要说一下我已经在谷歌和Stackoverflow上搜索了很多帖子,但是没有一个能够解决我的问题,因此我创建了一个新的帖子。
我正在创建一个具有两个子域的应用程序:account.psantos.dev和app.psantos.dev。
在config/initializers/session_store.rb
中,我有:
Rails.application.config.session_store :cookie_store, key: '_psantos.co.ao_session', domain: 'psantos.dev'
我有以下文件夹结构:
- app
-- controllers
--- app1
---- welcome_controller.rb
--- account
---- welcome_controller.rb
--views
--- layout
---- app1.html.erb
---- account.html.erb
--- app1
---- welcome
----- index.html.erb
--- account
---- welcome
----- index.html.erb
在app/views/layout/app1.html.erb
(http://app1.psantos.dev)中,我有以下一行:
<li><%= "Logout", account_sign_out_url, method: :delete %></li>
当我点击这个链接(将跳转到:http://account.psantos.dev),我收到了以下错误:
ActionController::InvalidAuthenticityToken at /sign_out
ActionController::InvalidAuthenticityToken
在两个布局文件(app1.html.erb和account.html.erb)的<\head >标签之前,我有以下行:<%= csrf_meta_tags %>
我该如何解决这个问题?
更新:相关日志
Started GET "/" for 127.0.0.1 at 2015-08-08 12:37:03 +0100 Processing
by APP1::WelcomeController#index as HTML Parameters:
{"subdomain"=>"app1"} Rendered app1/welcome/index.html.erb within
layouts/app1 (0.4ms) [1m[35mEntity Load (0.3ms)[0m SELECT
"entities".* FROM "entities" WHERE "entities"."user_token" = $1 LIMIT
1 [["user_token", "xxxxxxxxxxxxxxxxxxxx"]] Completed 200 OK in 43ms
(Views: 42.0ms | ActiveRecord: 0.3ms)
Started DELETE "/sign_out" for 127.0.0.1 at 2015-08-08 12:37:05 +0100
Processing by Account::SessionsController#destroy as HTML
Parameters: {"subdomain"=>"account"} Can't verify CSRF token
authenticity Completed 422 Unprocessable Entity in 1ms (ActiveRecord:
0.0ms)
ActionController::InvalidAuthenticityToken -
ActionController::InvalidAuthenticityToken: actionpack (4.2.3)
lib/action_controller/metal/request_forgery_protection.rb:181:in
`handle_unverified_request' actionpack (4.2.3)
lib/action_controller/metal/request_forgery_protection.rb:209:in
`handle_unverified_request' actionpack (4.2.3)
lib/action_controller/metal/request_forgery_protection.rb:204:in
`verify_authenticity_token' activesupport (4.2.3)
lib/active_support/callbacks.rb:430:in `block in make_lambda'
activesupport (4.2.3) lib/active_support/callbacks.rb:143:in `block in
halting_and_conditional' activesupport (4.2.3)
lib/active_support/callbacks.rb:502:in `block in call' activesupport
(4.2.3) lib/active_support/callbacks.rb:502:in `call'
app/controllers/application_controller.rb
中切换到protect_from_forgery with: :null_session
并阅读有关 Rails 中 CSRF 的内容。 - Roman Kiselenkorescue_from
创建自己的处理程序。 - Roman Kiselenko