Set filter before_action for ActiveAdmin controller

12,633

Solution 1

You can access the controller from within the controller do ... end DSL:

ActiveAdmin.register User do

  before_action :set_product, only: [:show, :edit, :update, :destroy]

  controller do
    def set_product
      @product = Product.find_by_name(params[:name])
    end
  end

end

Solution 2

You can store it in the config: config/initializers/active_admin.rb

ActiveAdmin.setup do |config|
  def do_something_awesome
  end

  config.before_action :do_something_awesome      
end
Share:
12,633
Mike Andrianov
Author by

Mike Andrianov

Updated on June 04, 2022

Comments

  • Mike Andrianov
    Mike Andrianov almost 2 years

    I want to add before_action filter to ActiveAdmin controller.

    Could I do something like this:

    before_action :set_product, only: [:show, :edit, :update, :destroy]
    
    private
    
    def set_product
      @product = Product.find_by_name(params[:name])
    end
    
  • Oscar Barrett
    Oscar Barrett over 8 years
    For newer versions of activeadmin, before_filter should be within the controller block.
  • arogachev
    arogachev about 8 years
    @OscarBarrett No, it works for me for the latest version of Active Admin (at the moment of writing this).