Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flash notice is lost on redirect, how to find out what's removing it?

There are many posts on SO about this ( respond_with redirect with notice flash message not working Why is :notice not showing after redirect in Rails 3, among others) , I've read at least 4 and still can't solve this issue.

I've got a portion of my site that lets people do some things before they create an account. I prefer this from a UX perspective. So they're allowed to do X and Y then they get redirected to the "Create account" page (uses Devise).

The redirect looks like:

if userIsNew 
  ... stow information in a cookie to be retrieved later ...     
  redirect_to "/flash", flash[:notice]  
    => "Ok, we'll get right on that after you sign up (we need your email)." 
      and return # this has to be here, since I'm terminating the action early
end 

So "/flash" is a plain page that I made to test this. It doesn't do anything, has no markup of its own, just has the basic html from the application.html, which has this line in the body:

 <% if flash[:notice] %>
    <p><%= notice %></p>
 <% else %>
  No notice!
 <% end %>

It says 'No notice' every time.

I have tried:

  • adding in a flash.keep to my before_filter in the static controller
  • using :notice => instead of flash[:notice] =>
  • putting the notice in a cookie and pulling that text out of the cookie and into a flash in the before_filter of my application controller
  • redirect_to :back with the flash[:notice] =>
like image 894
jcollum Avatar asked Nov 27 '22 18:11

jcollum


2 Answers

It's either

flash[:notice] = 'blablabla'
redirect_to foo_url

or

redirect_to foo_url, notice: 'blablabla'

like image 90
Damien Avatar answered Dec 07 '22 23:12

Damien


I'm overriding ApplicationController#redirect_to to call flash.keep so that any messages are persisted on redirect without having to explicitly call flash.keep in my controller actions. Works well so far. Haven't had a scenario yet where unwanted messages are persisted.

class ApplicationController < ActionController::Base
  def redirect_to(*args)
    flash.keep
    super
  end
end

Let me know if there are any scenarios where this isn't a good solution.

like image 37
joelvh Avatar answered Dec 08 '22 01:12

joelvh