新手问题 session 中的 flash 使用诡异问题

tiemei · March 29, 2013 · Last by Rei replied at March 29, 2013 · 4285 hits

如上, get 请求/articles/new 点击点击创建后,进入 create method,关联上当前用户创建 ariticle 并 save,这里的两个 flash 变量,始终无法传递到 redirect_to 后的 show 方法?

但是 RoR 明明是这么说的: By default, adding values to the flash will make them available to the next request,

不知大家有没有遇到过?

这颜色无法直视,看不清问题。

#1 楼 @Rei 已重新编辑

#2 楼 @tiemei 输出结果是什么?在 view 里能输出吗?

#3 楼 @Rei logger.debug 输出为空,页面

<%= flash[:n] %>

也为空

并且,走过一边 create 逻辑后,其他地方的 flash 也失效了

#3 楼 @Rei

HomeController < ... def from flash[:n] = 'set flash[:n] in home#from' flash.keep(:n) redirect_to '/to' end

def to end end router:

match 'from' => 'home#from' match 'to' => 'home#to'

view:

<%= flash[:n] %>

经过上面 create 那段逻辑后,这里请求 /from 跳转后的页面也为空

登出后,请求/from flash 工作恢复正常,再次登入也正常,但是走过一次 create 逻辑后,一切又失败了

登出逻辑:

def logout session[:current_user] = nil redirect_to '/login' end

#4 楼 @tiemei 发现楼主代码里直接往 session 和 flash 里存对象,session 和 flash 依赖 cookies 储存,是有 4KB 大小限制的,不知道是不是这个问题。

#6 楼 @Rei

config/initializers/session_store.rb

Blog::Application.config.session_store :active_record_store

难道是这边我的误用?

#7 楼 @tiemei 把 create action 的日志贴一下。create action 的开头和 redirect_to 之前加个

logger.debug flash.inspect

9 Floor has deleted

#8 楼 @Rei 图片太宽了,重新贴文字:

========before #, @closed=false, @flashes={}, @now=nil>

=========after #, @closed=false, @flashes={:article=>#, :n=>"dfasdfds"}, @now=nil>

#9 楼 @tiemei show 的 logger 还在吗?因为没看到日志。

#11 楼 @Rei 跳转后新请求这段日志

Redirected to http://localhost:3000/articles/33 Completed 302 Found in 15ms (ActiveRecord: 3.2ms)

Started GET "/articles/33" for 127.0.0.1 at 2013-03-29 15:52:10 +0800 Processing by ArticlesController#show as HTML Parameters: {"id"=>"33"} Article Load (0.3ms) SELECT articles.* FROM articles WHERE articles.id = 33 LIMIT 1 Rendered articles/show.html.erb within layouts/application (0.2ms) Completed 200 OK in 6ms (Views: 4.1ms | ActiveRecord: 0.3ms)

我又加了再 show 里加了一段 debug,却是 flash 被清空了

========in articles@show#, @closed=false, @flashes={}, @now=nil>

#12 楼 @tiemei 连 "flash[:n]=" 这个字符串都没出现,是不是有重名 action?

#13 楼 @Rei 不会重名 在 show action 加上

flash[:n] = 'set flash[:n] in articles#show'

debug 出这段

========in articles@show#, @closed=false, @flashes={:n=>"set flash[:n] in articles#show"}, @now=nil>

且 view 里的 flash[:n] 也生效了,一般不是要下面这种方式 same request 才生效的么?

flash.now[:n] = '....' 竟然改变了 flash 的默认 next request show 的行为

#14 楼 @tiemei 按着顶楼那样,让 flash 跟着 redirect 过来,打印一下 flash 是什么情况。

flash 就是一个存在 session 里的 hash,设了值可以马上访问。在某个 rack 中间件里会判断,已经有的就让它下次过期。

#15 楼 @Rei 这段竟然会报 undefined method `[]' for nil:NilClass?

logger.debug session

再 create 开头和 redirect 之前及 show 方法里安置 debug

输出:

========step1 in articles#create #, @closed=false, @flashes={}, @now=nil> #User:0x007fe0c6138298

========step2 in articles@create #, @closed=false, @flashes={:article=>#, :n=>"dfasdfds"}, @now=nil> #User:0x007fe0c6138298

========in articles#show #, @closed=false, @flashes={}, @now=nil> #User:0x007fe0c62403c0

日志贴全啊不要删减,只露一角怎么看真相呢?

有可能是 bug 了,rails 什么版本?升到最新看看。push 到 github 上我跑一跑。

#19 楼 @Rei

jiadongkais-MacBook-Pro:blog jiadongkai$ rails -v Rails 3.2.12 jiadongkais-MacBook-Pro:blog jiadongkai$ ruby -v ruby 2.0.0p0 (2013-02-24 revision 39474) [x86_64-darwin12.3.0] jiadongkais-MacBook-Pro:blog jiadongkai$ gem -v 2.0.3

复现步骤:

git clone https://github.com/tiemei/blog.git cd blog bundle install (修改 database.yml) rake db:migrate rails server

http://localhost:3000/users/new 注册新用户 http://localhost:3000/login 登陆 http://localhost:3000/articles/new 填写内容提交

发现问题了

> ActiveRecord::SessionStore::Session.last.data
  ActiveRecord::SessionStore::Session Load (0.2ms)  SELECT `sessions`.* FROM `sessions` ORDER BY `sessions`.`id` DESC LIMIT 1
ArgumentError: undefined class/module User

Session 在反序列化的时候,找不到 User 和 Article 类,导致解析失败。开发环境下 Model 每次都需要重新载入。

Session 里面不放对象是好习惯,如果一定要放就要打补丁,比较麻烦我就不说了。

You need to Sign in before reply, if you don't have an account, please Sign up first.