0

我有一个指南模型和一个评论模型,我试图让它们都可以在我的活动提要中进行跟踪。指导方针有效,评论无效。

2个主要问题是:

我不确定如何在activity_controller.rb 中添加“评论”作为第二个trackable_type,我不确定如何在comment/_create.rb 中对我的视图进行排序——它应该说评论已添加到x 指南(和链接该指南显示页面)。

指南.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

评论.rb

include PublicActivity::Model
  tracked owner: ->(controller, model){controller && controller.current_user}

  belongs_to :guideline
  belongs_to :commenter, class_name: 'User'

活动控制器.rb

def index
        @activities = PublicActivity::Activity
        .order("created_at desc")
        .where(trackable_type: 'Guideline' 'Comment')

意见/public_activity/comment/_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/guideline/_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
4

1 回答 1

0

如果我正确理解你的问题,这个演员解释得很好: http ://railscasts.com/episodes/406-public-activity ,以防你还没有看到它。

在我的情况下,我有Post模型(而不是你的GuidelinesComment,我刚刚添加了

  include PublicActivity::Model
  tracked owner: Proc.new{ |controller, model| controller.current_user }

和模型PostComment一切都运行良好,没有添加任何东西activities_controller

public_activity/comment/_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/post/_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 %>
于 2013-03-22T15:43:06.007 回答