Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how can I create a before_filter #1108

Closed
davidslvto opened this issue Mar 6, 2012 · 6 comments
Closed

how can I create a before_filter #1108

davidslvto opened this issue Mar 6, 2012 · 6 comments

Comments

@davidslvto
Copy link

Hi,

How can I create a before_filter only for some actions?

Thank you

@latortuga
Copy link
Contributor

Put it in a controller block. The code in a controller block is executed just as if it were inside a controller:

controller do
  before_filter :do_a_fancy_thing, :only => :index
end

If you want to have the before filter run before all active admin controllers/actions, you can put it in the AA initializer, there is clear documentation in the file about how to do that.

@davidslvto
Copy link
Author

Thank you

@casiodk
Copy link

casiodk commented Mar 11, 2013

how can you access the object that you are updating. for instance a instance of Post

@macfanatic
Copy link
Contributor

Unless you called prepend_before_filter, your filter should run after the ones provided by inherited_resources and therefore your resource should be set.

You could test this by:

ActiveAdmin.register Post do
  controller do
    before_filter :my_filter, only: [:edit, :show]

    private

    def my_filter
      Rails.logger.info "my post: '#{resource}'"
    end
  end
end  

@freeordeal
Copy link

Thanks @latortuga and @macfanatic. :)

@reinaris
Copy link
Contributor

@casiodk use resource

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants