存储在 Rails 会话中的对象变成字符串? [英] Object stored in Rails session becomes a String?

查看:51
本文介绍了存储在 Rails 会话中的对象变成字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Normally I wouldn't store objects in a Rails session but I'm using a library that requires this. I've run into a very strange issue where a stored object appears as a String after redirect.

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

To reproduce I've created a sample Rails 4.1 app

$ rails new session-test

添加了一个测试控制器:

Added a test controller:

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

Then I fire up Rails

$ bundle exec rails server

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

and hit localhost:3000/home/from in the browser:

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'

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

I have no idea why this object is getting translated as a String...

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

It appears to have to do with the session store type of cookie_store because if I change

session_store.rb 来自

session_store.rb from

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

Rails.application.config.session_store :cache_store

它有效!

有什么想法吗?

推荐答案

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

You can't store objects in the Rails session. It's a key-value store that only accepts strings because, more often than not, it's packaged up and sent to the client as an encrypted cookie.

这不是您可能需要的东西的倾倒场.注意你在里面塞了多少垃圾,因为你越依赖会话,cookie 越大,客户端必须为每个请求返回到你的服务器.

It's not a dumping ground for things you might need. Pay attention to how much junk you cram in there because the more you lean on the session, the larger the cookie is that the client will have to sling back to your server for every request.

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

It's worth observing the headers in your browser's network inspection tool to see how heavy a footprint your requests have.

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

If you really do need to persist something in there, use a string-friendly encoding format like JSON to be sure you can get the data back out in a usable format.

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

I'd be very hesitant to use the cache_store as well, that doesn't get shared across different instances of your application. Ruby objects only exist within the context of a single process, so other requests, which more often than not will hit some random process, will not be able to make use of this as easily.

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

The default cookie store is the most reliable. The others that share between processes are dependent on additional services being operational (Memcached, Redis, etc.) but most of those dictate a strings-only policy as well.

这篇关于存储在 Rails 会话中的对象变成字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆