Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: prepend_before_action in superclass

I have an authentication method in my ApplicationController that I always want to run first. I also have a method in an subcontroller that I want to run after the authentication method, but before the other ApplicationController before_actions. In other words, I want this:

ApplicationController
before_action first
before_action third

OtherController < ApplicationController
before_action second

The above causes the methods to be called in order of: first -> third -> second. But I want the order to go: first -> second -> third.

I've tried using prepend_before_action, like so:

ApplicationController
prepend_before_action first
before_action third

OtherController < ApplicationController
prepend_before_action second

But this causes it to go second -> first -> third.

How do I get the order to be first -> second -> third?

like image 306
Robert Avatar asked Feb 22 '16 18:02

Robert


Video Answer


2 Answers

You can use the prepend_before_actionlike this:

class ApplicationController < ActionController::Base
  before_action :first
  before_action :third
end

class OtherController < ApplicationController
  prepend_before_action :third, :second
end
like image 50
Samuel G. P. Avatar answered Oct 07 '22 01:10

Samuel G. P.


Try the following, and see if it works:

class ApplicationController < ActionController::Base
  before_action :first
  before_action :third
end

class OtherController < ApplicationController
  skip_before_action :third
  before_action :second
  before_action :third
end
like image 37
Dharam Gollapudi Avatar answered Oct 07 '22 00:10

Dharam Gollapudi