6

I updated to Rails 2.3.10, Rack 1.2.1 and now none of my flash messages are showing up. I found that during a redirect the notice is passed in like this

redirect_to(@user, :notice => "Sorry there was an error")

And in my view the flash hash is empty

<%= debug flash %>  
!map:ActionController::Flash::FlashHash {} 

But you can see the message in the controller. What gives?

        <%= debug controller.session %>
        session{:home_zip=>"94108", :session_id=>"xxx", :flash=>{:notice=>"Sorry there was an error"}, :user_credentials=>"1baf9c9c0423ce0151ec32e24cc422f07309e4ba503eb4830635ecc115da217809997324374bb273b3fb792895c9741a8b8c9ea4267771a1bd149de5b9179ea0", :user_credentials_id=>22, :home_market_id=>11}
        Edit Profile    
jspooner
  • 10,975
  • 11
  • 58
  • 81

4 Answers4

4

The code below should work:

redirect_to(@user, {:notice => "Sorry there was an error"})

I'm guessing this is due to changes in Ruby and not in Rails, because it looks like a token parsing priority change in the compiler.

barbolo
  • 3,807
  • 1
  • 31
  • 31
  • 1
    It worked for me, in my case it was a `model_path model` before the flash that was causing the trouble. – mtrovo Feb 18 '13 at 16:32
4

Did you check the rails bug tracker? I still use the old fashioned setter flash[:notice] = message and it works fine, so it seems to be a redirect_to method problem.

https://rails.lighthouseapp.com/

Did you try redirect_to url, :flash => { :notice => "notice" }, as a work around?

Max Schulze
  • 1,173
  • 7
  • 8
  • Thanks but I tried that too but it looks like the flash method that is called from the view is broken. – jspooner Oct 26 '10 at 04:56
1

This could be an issue with cookies. Long story short, cookies don't get if you redirect immediately afterwards. Assuming that Rails implements flash using cookies, the redirect is your problem.

Sources:

http://persistall.com/archive/2008/01/25/cookies--redirects--nightmares.aspx http://stackoverflow.com/questions/1621499/why-cant-i-set-a-cookie-and-redirect

Vlad the Impala
  • 15,572
  • 16
  • 81
  • 124
1

We just ran into this too. All our flash messages disappear with redirect, but not when set in the controller explicitly.

Does not work:

  def create
    if @obj.save
      flash[:notice] = "The #{cname.humanize.downcase} has been created."
      redirect_back_or_default redirect_url
    else
      render :action => 'new'
    end
  end

This does work:

 def show
    @user = current_user
    flash[:notice] = "Hello -- this will show up fine"
  end
Charles Forcey
  • 537
  • 5
  • 8