brycemcd
brycemcd

Reputation: 4543

How to access flash messages in rails metal

One of my actions is page cached so the server is only reading the HTML. There are just a few parts that are dynamic and I access those parts via an AJAX call to a rails metal endpoint.

I'm trying to return the flash[:notice] and/or flash[:alert] notices in the call as well but I keep getting a cryptic error (below). Is there a way to access flash messages via rails metal?

The code in my metal class is:

json = "{flash : '#{flash[:notice]}' , user_balance : #{@user.balance.to_i}, cart_items : #{bal} }"
[200, {"Content-Type" => "application/json"}, [json] ]

And the error:

Thu Feb 03 13:01:20 -0800 2011: Read error: #<NoMethodError: undefined method `bytesize' for nil:NilClass>
/home/brycemcd/.rvm/gems/ruby-1.8.7-p330/gems/activesupport-2.3.5/lib/active_support/whiny_nil.rb:52:in `method_missing'
/home/brycemcd/.rvm/gems/ruby-1.8.7-p330/gems/rack-1.0.1/lib/rack/utils.rb:156:in `bytesize'
/home/brycemcd/.rvm/gems/ruby-1.8.7-p330/gems/rack-1.0.1/lib/rack/content_length.rb:22:in `call'
/home/brycemcd/Projects/theclymb/app/metal/presentation_cache.rb:29:in `inject'

Upvotes: 1

Views: 856

Answers (1)

fl00r
fl00r

Reputation: 83680

You can find your flash messages in rack.session. Using Rack it is in env variable. rack.session contains flash hash: env["rack.session"]["flash"]["error"] or env["rack.session"]["flash"]["notice"] or anything else.

match "/hello" => proc { |env| [200, {}, "#{env['rack.session']['flash']['error']}"] }, :as => :hello

Upvotes: 1

Related Questions