2017-10-17 123 views
2

我想让https为我的rails应用程序工作。当我尝试在应用程序上执行任何与帖子相关的活动时,我收到以下错误消息。HTTPS for Rails应用程序 - InvalidAuthenticityToken

Exception `ActionController::InvalidAuthenticityToken' at /usr/local/rvm/gems/ruby-2.4.1/gems/actionpack-5.0.4/lib/action_controller/metal/request_forgery_protection.rb:195 - ActionCon 
troller::InvalidAuthenticityToken 
Exception `ActionController::InvalidAuthenticityToken' at /usr/local/rvm/gems/ruby-2.4.1/gems/actionpack-5.0.4/lib/action_controller/metal/rescue.rb:23 - ActionController::InvalidAuthe 
nticityToken 
Exception `ActionController::InvalidAuthenticityToken' at /usr/local/rvm/gems/ruby-2.4.1/gems/activesupport-5.0.4/lib/active_support/notifications/instrumenter.rb:25 - ActionController 
::InvalidAuthenticityToken 
Exception `ActionController::InvalidAuthenticityToken' at /usr/local/rvm/gems/ruby-2.4.1/gems/actionpack-5.0.4/lib/action_dispatch/middleware/callbacks.rb:42 - ActionController::Invali 
dAuthenticityToken 
Exception `ActionController::InvalidAuthenticityToken' at /usr/local/rvm/gems/ruby-2.4.1/gems/actionpack-5.0.4/lib/action_dispatch/middleware/debug_exceptions.rb:77 - ActionController: 
:InvalidAuthenticityToken 
+0

你可以发布你的控制器代码的副本吗?你是否在使用任何认证宝石,如Devise?如果是这样,哪个版本?另外,这项工作没有SSL吗? –

+0

@TomAranda它适用于http。当我发布其发布到http表单提交从https –

+0

您的整个会话在https上?换句话说,您的所有请求是否加载表单GET并通过https发布表单?如果不是,那么rails可能会使用不同的会话,导致真实性令牌失败。 –

回答

2

尝试添加以下内容到config/environments/production.rb

# config/environments/production.rb 
Rails.application.configure do 
    config.force_ssl = true 
end 

如果你想这才会在其他环境影响,将其添加到 适当的环境文件中。

+0

让我测试一下:) –

+0

真棒这个作品。不得不添加'proxy_set_header X-Forwarded-Proto $ scheme;'在nginx配置以及:) –

+0

我很高兴它的工作。 –