Search code examples
ruby-on-railsbefore-filter

Can you call a before_filter from another before_filter in Ruby on Rails?


Is it possible to call a before_filter from another? The reason I ask is because I have two similar before_filters, but one has an extra condition that could still let it be true. So, if the first before_filter is true than the second one is true as well, but if the first is false another condition is checked and then decided if it's true or false.


Solution

  • It is correct that filters are just methods, so to answer your question:

    Yes, it is possible to call filters from other filter.

    BUT beware that you should not render or redirect twice, otherwise you will receive a AbstractController::DoubleRenderError. Thus if one of the filters is rendering you should not call the other filter.

    Use boolean return values to manage this.