![](/img/trans.png)
[英]disable public_activity tracking for sidekiq jobs that require current_user
[英]How to access current_user in partial of public_activity gem?
當我嘗試訪問部分文件中的current_user時,它將包含nil值。
我將devise gem用於登錄過程。 我還使用public_activity gem生成通知。
我有如下的通知控制器。
def index
@activities = PublicActivity::Activity.all
end
在views / notifications / index.html.erb中
<%= render @activities %>
現在在views / public_activity / commontator_comment / _create.html.erb中
在此部分中,我要訪問current_user,但其中包含nil值。
我不明白是什么問題。
請幫我。 提前致謝。
在應用程序控制器中,您需要包含一個模塊
class ApplicationController < ActionController::Base
include PublicActivity::StoreController
end
這會記錄每個請求的控制器,使我們可以從模型中訪問它。 我們可以在模型中通過向跟蹤添加所有者選項來做到這一點。
tracked owner: ->(controller, model) { controller.current_user }
您可以在此處詳細了解此信息http://asciicasts.com/episodes/406-public-activity
跟蹤用戶示例。
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
應用程序/視圖/活動/ index.html.haml
- @activities.each do |a|
= render_activity a
其他意見
/app
/views
/public_activity
/user
_create.html.haml
_destroy.html.haml
[_all_methods.html.haml]
查看范例
= 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.