Search code examples
ruby-on-railsrubydeviserails-admin

Rails_admin, Assigning Devise's current_user to model inside action


I have Devise Model Person which will log in and manage rails_admin app.

I have model called Model which has updater that was added by mongoid-history.

In order to set who updated my story I need to do something like this:

model = Model.new
model.updater = Person.first
model.save

According to this link to github, I can not set current_person, which created by Devise automatically. That's means that I need to set updater manually each time when something happens to my Model.

How to get current_person and set it to Model where rails_admin's action happens?

I only know that I need to write something to each action in initializers/rails_admin.rb


Solution

  • I have found one solution for my question, but I don't know if it is good aproach or not.

    If I want to assign my current_person in edit action I needed to write this:

    config.model Model do
    
       edit do
         field :updater_id, :hidden do
         visible true
         def value
           bindings[:view]._current_user.id
         end
        end
       end
    
    end
    

    It createds hidden field which has asigned ID of current person which is logged in.