Search code examples
ruby-on-railsrails-admin

Trying to access current user in rails admin


Is there a way to get access to the current_user in rails admin config.model filterable option. I have tried using:

bindings[:view]._current_user

but bindings is nil for me

Here's a code snippet of what I've been doing:

field :user do
  filterable do                     #I want to access current user here
    bindings[:view]._current_user   #bindings is nil
  end

  pretty_value do
    bindings[:view]._current_user   #bindings is not nil
  end
end

Solution

  • I couldn't access it via the bindings approach so I just made a before filter in my application controller that calls a method there which sets the current user in my User model via a class method.

    Thus, in my code snippet, I could access current_user like so:

    ...
    filterable do
    User.current
    end
    ...
    

    Answer came from this: https://amitrmohanty.wordpress.com/2014/01/20/how-to-get-current_user-in-model-and-observer-rails/