2

我发现了许多描述如何做到这一点的帖子。它们看起来都像是把它放在适当的环境配置文件中:

config.action_controller.session[:domain] = '.localhost'

但是,如果我这样做,那么尝试登录(我正在使用设计)会失败:

ActionController::InvalidAuthenticityToken

我看到其他人发布了同样的问题(在各种博客的评论部分提供了设置会话 [:域] 的建议),但我还没有发现有人回答了关于为什么会发生这种情况以及如何解决的问题的案例它。

有任何想法吗?

4

2 回答 2

1

我在 config/initializers/set_session_domain.rb 中有这个片段:

module ActionControllerExtensions
  def self.included(base)
    base::Dispatcher.send :include, DispatcherExtensions
  end

  module DispatcherExtensions
    def self.included(base)
      base.send :before_dispatch, :set_session_domain
    end

    def set_session_domain
      domain = @env['HTTP_HOST'].gsub(/:\d+$/, '').gsub(/^[^.]*/, '')
      @env['rack.session.options'].update :domain => domain
    end
  end
end

ActionController.send :include, ActionControllerExtensions

一切都很好。

于 2010-09-19T11:32:29.253 回答
1

我不确定这是否与您的问题有关,但您是否尝试将会话域设置为“.localhost”?这将不起作用,因为它实际上是您尝试为其设置 cookie 的顶级域。

http://www.ruby-forum.com/topic/181650#794923

于 2010-09-19T10:19:47.057 回答