0

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.

sank
  • 964
  • 3
  • 12
  • 24

2 Answers2

0

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

Rajdeep Singh
  • 17,621
  • 6
  • 53
  • 78
0

Example for track User.

application_controller.rb

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

  [.....]

  # Only if devise is not used
  def current_user
    @current_user ||= User.find(session[:user_id]) if session[:user_id]
  end
end

activities_controller.rb

class ActivitiesController < ApplicationController
  def index
    @activities = PublicActivity::Activity.order("created_at desc")
  end
end

user.rb

class User < ActiveRecord::Base
  include PublicActivity::Common
  [...]
end

users_controller.rb

class UsersController < ApplicationController      

  def my_method
    [...]
      respond_to do |format|
      if @my_object.save
      @my_object.create_activity :create, owner: current_user 
    [...] 
  end
end

app/views/activities/index.html.haml

- @activities.each do |a|
  = render_activity a

others views

/app
  /views
    /public_activity
      /user
        _create.html.haml
        _destroy.html.haml
        [_all_methods.html.haml] 

view example

 = a.owner.name + " " + a.owner.firstname
 created the user
 = a.trackable.name + " " + a.trackable.firstname + " (" + a.trackable.email + ")"