Frank Koehl
Frank Koehl

Reputation: 3186

Rails flash.now within after_action

Code on this one speaks for itself.

This works...

class SomeController < ApplicationController

  def index
    flash.now[:alert] = 'Something got borked!'
  end

end

And this doesn't (flash is empty)...

class SomeController < ApplicationController

  after_action :test_flash

  def index
  end

  def test_flash
    flash.now[:alert] = 'Something got borked!'
  end

end

Using Rails 4.0.2, Ruby 2.1.0

Thoughts anyone?

Upvotes: 1

Views: 621

Answers (1)

Kirti Thorat
Kirti Thorat

Reputation: 53038

At the end of your index action rails just renders the new page as you have not specified anything different. after_action is executed after rendering the index page i.e., it adds a callback which would be called after the execution of index action is completed. So, test_flash has no effect as its too late and index page is already rendered.

EDIT:

If you want to call test_flash before rendering then you can override render method and do something like :

  def render *args
    test_flash
    super
  end

Upvotes: 2

Related Questions