How do you add a filter to the very end of the superclass filter chain?

in rails, when you register filters in abstract superclasses, they appear before the filters registered in the controller class. Let's say I wanted to execute a method called authenticate as a filter right at the end of the filter chain. The only way to understand this is to declare that before_filter as the last filter in all my controller classes (and there are many). Is there a way to declare this filter in a superclass and execute it the last? The reason I want the latter to be executed is because the controller class can change the authentication requirements for that controller only, and I want these changes to be taken into account before the final authentication filter is called.

+1
source share
2 answers

Use prepend_before_filter in your controller classes instead of before_filter or append_before_filter .

+2
source

Rails first calls your ApplicationController, before the local ... so you can do something like this (using your example):

In your application controller, you will have a before_filter and the corresponding method that is called:

 before_filter :authenticate def authenticate # do something end 

In the controller for the type of resource you are working with ...

You can override / override authenticate

 def authenticate # do something else end 

You can even choose NOT to use authenticate callback for some methods

 skip_before_filter :authenticate, :only => :my_method_without_auth 
0
source

Source: https://habr.com/ru/post/890834/


All Articles