为ActiveAdmin控制器设置过滤器before_action

时间:2014-02-06 14:20:14

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4 activeadmin

我想将 before_action 过滤器添加到ActiveAdmin控制器。

我可以这样做:

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

private

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

2 个答案:

答案 0 :(得分:14)

您可以从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

答案 1 :(得分:0)

您可以将其存储在配置中:config/initializers/active_admin.rb

ActiveAdmin.setup do |config|
  def do_something_awesome
  end

  config.before_action :do_something_awesome      
end