ruby-on-railsrubyruby-on-rails-3devisepublic-activity

How to access current_user in partial of public_activity gem?


When I m trying to access current_user in partial file it will contains nil value.

I used devise gem for login process. I also used public_activity gem for generating notification.

I have notification controller as below.

def index
    @activities = PublicActivity::Activity.all      
end

In views/notifications/index.html.erb

<%= render @activities %>

Now in views/public_activity/commontator_comment/_create.html.erb

in this partial I want to access current_user but it contains nil value.

I didn't understand what was the problem.

please help me. thanks in advance.


Solution

  • In application controller you need to include a module

    class ApplicationController < ActionController::Base  
      include PublicActivity::StoreController       
    
    end 
    

    This records the controller on each request allowing us to access it from the models. We can do this in the model by adding an owner option to tracked.

    tracked owner: ->(controller, model) { controller.current_user } 
    

    You can read in details about this here http://asciicasts.com/episodes/406-public-activity