简体   繁体   English

如何在public_activity gem中访问current_user?

[英]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. 当我尝试访问部分文件中的current_user时,它将包含nil值。

I used devise gem for login process. 我将devise gem用于登录过程。 I also used public_activity gem for generating notification. 我还使用public_activity gem生成通知。

I have notification controller as below. 我有如下的通知控制器。

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

In views/notifications/index.html.erb 在views / notifications / index.html.erb中

<%= render @activities %>

Now in views/public_activity/commontator_comment/_create.html.erb 现在在views / public_activity / commontator_comment / _create.html.erb中

in this partial I want to access current_user but it contains nil value. 在此部分中,我要访问current_user,但其中包含nil值。

I didn't understand what was the problem. 我不明白是什么问题。

please help me. 请帮我。 thanks in advance. 提前致谢。

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 您可以在此处详细了解此信息http://asciicasts.com/episodes/406-public-activity

Example for track User. 跟踪用户示例。

application_controller.rb 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 activities_controller.rb

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

user.rb user.rb

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

users_controller.rb 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 应用程序/视图/活动/ 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 + ")"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM