Rails, losing flash after redirect_to
ruby-on-rails
Solution
When you use the flash messages feature, there are two ways of displaying messages:
Instantly on the same page load, and accessible in the view from `flash['foo']`:
flash.now['foo'] = "Hello world"
Or on a redirect to another page, and accessible from `flash['notice']`:
redirect_to root_url, notice: "Hello world"
The ruby on rails guides website is a really good reference:
http://guides.rubyonrails.org/action_controller_overview.html#the-flash
Problem
I can't figure out why my flash messages disappear after a redirect_to. Started the debugger in my view, and the flash variable is totally empty. ``` flash => {} ``` The result is the same with flash.now... It works fine if I edit something and call render. Controller: ``` def create @user_session = UserSession.new(params[:user_session]) if @user_session.save flash[:notice] = "Logged in" redirect_to root_url else render :action => 'new' end end ``` Application layout: ``` - flash.each do |name, msg| =content_tag :div, msg, :class => "flash_#{name}" ``` root_url is another controller and action.