小编Zac*_*ler的帖子

存储在Rails会话中的对象变成了一个String?

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

为了重现我已经创建了一个示例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
Run Code Online (Sandbox Code Playgroud)

设置路线:

Rails.application.routes.draw do
  get 'home/index'
  get 'home/from'
  root 'home#index'
end
Run Code Online (Sandbox Code Playgroud)

然后我启动Rails

$ bundle exec rails server

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

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: …
Run Code Online (Sandbox Code Playgroud)

session ruby-on-rails shopify ruby-on-rails-4 ruby-on-rails-4.1

8
推荐指数
1
解决办法
4620
查看次数