45

Suppose I have some logic in a base controller to pass information to the view to build something like a breadcrumb:

class ContextAwareController < ApplicationController

  after_filter :build_breadcrumb

  def build_breadcumb
    #...
  end
end

I want this build_breadcrumb method to run after the main controller logic, but before the view is rendered.

The above code runs too late, but a before_filter would be too early.

Can anybody suggest a way to accomplish this without explicitly calling build_breadcumb at the end of each of the actions in the child controllers?

Thanks

6 Answers 6

58

I had the same problem and solved it like this:

class ApplicationController < ActionController::Base
  def render *args
    add_breadcrumbs
    super
  end
end
3
  • 2
    But this solution would slow down every render.
    – freemanoid
    Jun 4, 2013 at 13:10
  • 2
    It would only slow down the renders made from a particular controller. It would be easy enough to move this to a different controller or "sub-controller" so that only the actions that need breadcrumbs use this version of render. Jul 8, 2013 at 14:59
  • We can also use a Controller Concern to add this functionality selectively elegantbrew.tumblr.com/post/70990048275/…
    – MhdSyrwan
    Jan 8, 2016 at 14:28
3

There are also some gems to achieve this. One of them is rails3_before_render. It works similarly to filters, for example:

class PostsController < ApplicationController
  before_render :ping, :except => [:destroy]

  def index; end
  def new; end
  def show; end
  def destroy; end                                                                          

  private
    def ping
      Rails.logger.info "Ping-Pong actions"
    end
end

(code snipped copied from gem documentation)

1
  • 3
    It's also worth mentioning that this filter is executed only if you call render method, thus before_render should not be used to update data in database when there is a possibility of redirect.
    – knarewski
    Feb 12, 2013 at 12:04
2

I believe rendering starts when render is called, and there's no default way to defer it. Here's one thing you could do:

filters are applied in the same order declared. So make a second after-filter that calls render with an array args stored in a class variable. Then anywhere you would normally call render, set the variable.

2

If we're overriding render, we're not really using the filter chain at all, so it might be simpler to determine which action we're in using the @_action_name.

StuffController < ApplicationController

  def my_filter
    # Do the stuff
  end

  def render(*args)
    my_filter if @_action_name == "show"
    super
  end

end
0

You can use alias_method_chain like this

alias_method_chain :render, :before_render_action

this will create 2 methods :render_with_before_render_action and :render_without_before_render_action. If you call render, then :render_with_before_render_action will be called. You can override this method

def render_with_before_render_action(*options, &block)
<your code>
render_without_before_render_action(*options, &block)
end

If you don't want your code to be executed and you want to have default render then you should directly call the :render_without_before_render_action

-1

You can do like this to fake a before_render:

class StuffController < ApplicationController
  before_filter :my_filter, only: [:index, :show]

  def my_filter
    @my_filter = true
  end
  def _my_filter
    # Do the actual stuff here
  end
  def render(*args)
    _my_filter if @my_filter
    super
  end
end

Thanks to @joshua-muheim for the tip about using render

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.