我有一个指南模型和一个评论模型,我试图让它们在我的活动Feed中都可以跟踪。指南正在起作用,评论不是。
两个主要问题是:
我不确定如何在activities_controller.rb中添加'Comment'作为第二个trackable_type,我不确定如何在comment / _create.rb中对我的视图进行排序 - 应该说在x指南中添加了注释(并链接该指南显示页面)。
guidelines.rb
include PublicActivity::Model
tracked owner: ->(controller, model){controller && controller.current_user}
attr_accessible :content, :hospital, :title, :user_id, :guideline_id, :specialty, :updated_by, :current_user, :subtitle, :slug, :activities, :comment
belongs_to :user
has_many :favourite_guidelines
has_many :comments, :dependent => :destroy
comments.rb
include PublicActivity::Model
tracked owner: ->(controller, model){controller && controller.current_user}
belongs_to :guideline
belongs_to :commenter, class_name: 'User'
activities_controller.rb
def index
@activities = PublicActivity::Activity
.order("created_at desc")
.where(trackable_type: 'Guideline' 'Comment')
视图/ public_activity /评论/ _create.html.erb
added a comment
<% if activity.trackable %>
to the guideline <%= link_to activity.trackable.body, activity.trackable %>
<% else %>
which can no longer be viewed
<% end %>
视图/ public_activity /准则/ _create.html.erb
added a guideline
<% if activity.trackable %>
titled <%= link_to activity.trackable.title, activity.trackable %>
<% else %>
which can no longer be viewed
<% end %>
我的routes.rb是
Guidelines::Application.routes.draw do
get "activities/index"
# get "user/index"
ActiveAdmin.routes(self)
devise_for :admin_user, ActiveAdmin::Devise.config
get "guidelines/topic"
get "guidelines/topichospital"
get "guidelines/topicspecialty"
get "guidelines/favourite"
get "profiles/show"
get "guidelines/show"
devise_for :users
devise_scope :user do
get 'signup', to: 'devise/registrations#new', as: :register
get 'login', to: 'devise/sessions#new', as: :login
get 'logout', to: 'devise/sessions#destroy', as: :logout
get 'edit', to: 'devise/registrations#edit', as: :edit
put 'users' => 'devise/registrations#update', :as => 'user_registration'
get 'about', to: 'about#about', as: :about
end
resources :guidelines
get 'guidelines', to: 'guidelines#index', as: :guidelines
get 'favourites', to: "favourites#show", as: :favourites
get 'topics', to: 'guidelines#list', as: :topics
get 'hospitals', to: 'guidelines#listhospital', as: :hospitals
get 'specialties', to: 'guidelines#listspecialty', as: :specialties
resources :activities
root :to => 'guidelines#index'
resources :guidelines do
resources :comments
end
答案 0 :(得分:0)
如果我理解你的问题,这个演员解释得非常好: http://railscasts.com/episodes/406-public-activity如果您还没有看到它。
在我的情况下,我有Post
型号(而不是您的Guidelines
)和Comment
,我刚刚添加了
include PublicActivity::Model
tracked owner: Proc.new{ |controller, model| controller.current_user }
同时适用于Post
和Comment
模式,一切运作良好,无需向activities_controller
添加任何内容
<强> public_activity /评论/ _create.html.erb 强>
said "<%= activity.trackable.body.truncate(100) %>" on <%= link_to activity.trackable.commentable.title, project_post_path(activity.trackable.commentable.project, activity.trackable.commentable.id) %> post.
<强> public_activity /后/ _create.html.erb 强>
added a new <%= link_to activity.trackable_type.downcase, project_post_path(activity.trackable.project, activity.trackable.id) %> to <%= link_to activity.trackable.project.title, project_path(activity.trackable.project) %> project.
活动index.html.erb
<% @activities.each do |activity| %>
<%= link_to activity.owner.full_name, activity.owner if activity.owner %>
<%= render_activity(activity) %>
<% end %>