8

通常我不会在 Rails 会话中存储对象,但我使用的库需要这个。我遇到了一个非常奇怪的问题,其中存储的对象在重定向后显示为字符串。

为了重现我创建了一个示例 Rails 4.1 应用程序

$ rails new session-test

添加了一个测试控制器:

class HomeController < ApplicationController
  def index
    logger.debug "session[:customer]: #{session[:customer]}"
    logger.debug "session[:customer].name: #{session[:customer].name}"
  end

  def from
    Struct.new 'Customer', :name, :address
    session[:customer] = Struct::Customer.new 'Dave', '123 Main'
    redirect_to :action => :index
  end
end

设置路线:

Rails.application.routes.draw do
  get 'home/index'
  get 'home/from'
  root 'home#index'
end

然后我启动 Rails

$ bundle exec rails server

并在浏览器中点击 localhost:3000/home/from:

Started GET "/home/from" for 127.0.0.1 at 2014-04-09 21:20:25 -0700
Processing by HomeController#from as HTML
Redirected to http://localhost:3000/home/index
Completed 302 Found in 18ms (ActiveRecord: 0.0ms)


Started GET "/home/index" for 127.0.0.1 at 2014-04-09 21:20:25 -0700
Processing by HomeController#index as HTML
session[:customer]: #<struct Struct::Customer name="Dave", address="123 Main">
Completed 500 Internal Server Error in 2ms

NoMethodError (undefined method `name' for "#<struct Struct::Customer name=\"Dave\", address=\"123 Main\">":String):
  app/controllers/home_controller.rb:4:in `index'

我不知道为什么这个对象被翻译为字符串......

它似乎与 cookie_store 的会话存储类型有关,因为如果我更改

session_store.rb 来自

Rails.application.config.session_store :cookie_store, key: '_session-test_session'

Rails.application.config.session_store :cache_store

有用!

有任何想法吗?

4

3 回答 3

5

您不能在 Rails 会话中存储对象。它是一个只接受字符串的键值存储,因为它通常被打包并作为加密的 cookie 发送给客户端。

它不是你可能需要的东西的垃圾场。注意你在里面塞进了多少垃圾,因为你越依赖会话,cookie 就越大,客户端将不得不为每个请求返回到你的服务器。

值得观察浏览器网络检查工具中的标头,以了解您的请求占用的空间有多大。

如果您确实需要在其中保留某些内容,请使用字符串友好的编码格式(如 JSON),以确保您可以以可用的格式获取数据。

我也很犹豫是否要使用它cache_store,它不会在您的应用程序的不同实例之间共享。Ruby 对象只存在于单个进程的上下文中,因此其他请求(通常会遇到一些随机进程)将无法轻松利用它。

默认的 cookie 存储是最可靠的。在进程之间共享的其他服务取决于正在运行的其他服务(Memcached、Redis 等),但其中大多数也规定了仅字符串策略。

于 2014-04-10T04:38:36.270 回答
4

修复是手动显式执行序列化和反序列化。

例如

# storing...
session[:customer] = (Struct::Customer.new 'Dave', '123 Main').to_yaml

# retrieving...
customer = YAML.load(session[:customer])

对于shopify_appgem,请参阅拉取请求中的文件更改https://github.com/Shopify/shopify_app/pull/90/files并相应地应用于您现有的应用程序。

于 2014-05-02T14:08:24.260 回答
0

我的 Shopify 应用程序也遇到了同样的问题,并且花了很多时间调试它。基于 tadman 的准确解释(如果不是教条式的解释),我能够通过编组会话对象来解决它。它只需要 3 行代码即可更改:

login_protection.rb:12: ShopifyAPI::Base.activate_session(Marshal::load session[:shopify]) login_protection.rb:25: Marshal::load session[:shopify]

session_controller.rb:13: session[:shopify] = Marshal::dump(sess)

我在 Shopify 论坛上发布了此内容,并希望他们很快会发布更新。对于它的价值,我认为旧方法虽然在哲学上是“错误的”,但在 Rails 的每个版本中都能正常工作,直到 4.1。

于 2014-04-20T16:02:06.120 回答