Daniel Schobel
Daniel Schobel

Reputation: 506

redirecting a POST request in ruby-on-rails causes flash hash to be wiped out

When trying to redirect a POST request I'm finding that rails' flash hash is getting wiped out.

The redirect works fine when the initial request is a GET.

in config/routes.rb

post 'test/hello' 
#get 'test/hello' #uncomment this line and comment out the post and the flash is preserved
get 'test/goodbye'

in app/controllers/test_controller.rb

def hello
    respond_to do |format|
        format.html do
            redirect_to movies_goodbye_url, :notice => "I disappear on post requests"
        end
    end
end

def goodbye
end

in app/views/test/goodbye.html.haml

%h1 Test#goodbye
%p#alert= alert
%p#notice= notice

I want this action to handle posts from a form submission which redirects in error cases to one of my existing views. I'm pretty new to rails so if there's a better way to handle this use-case, please do let me know.

Upvotes: 1

Views: 439

Answers (1)

Chris Salzberg
Chris Salzberg

Reputation: 27374

Try adding a flash.keep in there:

flash.keep(:notice)
redirect_to movies_goodbye_url, :notice => "I disappear on post requests"

See also: Flash message in redirect not working

Upvotes: 2

Related Questions