class ParentController < ApplicationController
before_action admin_user, only: [:create, :update]
end
class ChildController < ParentController
before_action admin_user #Also want to add :tweet
def tweet
end
end
In the ChildController we could use
before_action admin_user, only: [:create, :update, :tweet]
but then if I change anything in the parent's before_action, it won't get updated in the child.
You could call the method inside of a block inside ChildController instead of passing it by name. That would look like this:
class ParentController < ApplicationController
before_action admin_user, only: [:create, :update]
end
class ChildController < ParentController
before_action(only: [:tweet]) { admin_user }
def tweet
end
end
Unfortunately, the way ActiveSupport::Callbacks
is implemented does not allow an additional action to be easily added to the configuration of the before filter. You could do this though:
class ParentController < ApplicationController
AdminActions = [:create, :update]
before_action :admin_user, only: AdminActions
end
class ChildController < ParentController
before_action :admin_user, only: superclass::AdminActions + [:tweet]
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With