0

我有一个简单的 Rails 应用程序,我正在尝试在其上添加活动源。为此,我正在使用 Public Activity Gem 并关注此Railscast

但现在它给了我一个错误:

undefined method `shipment' for #<Shipment:0x6a96578>

我的代码是:

活动控制器.rb

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

我的活动 index.html.erb 文件

<h1>feeds</h1>
<% @activities.each do |activity| %>
  <div class="activity">
    <%= link_to activity.owner.full_name, activity.owner if activity.owner %>
    added comment to <%= link_to activity.trackable.shipment.name.activity.trackable.shipment %>
    </div>
    <% end %>

我的comment.rb 文件

class Comment < ActiveRecord::Base
include PublicActivity::Model
 tracked owner: ->(controller, model) { controller && controller.current_user }
  belongs_to :shipment
  belongs_to :user
end

我有一个简单的 Rails 应用程序,我正在尝试在其上添加活动源。为此,我正在使用 Public Activity Gem 并关注此 Railscast

但现在它给了我一个错误:

#<#:0x4a90a30> 的未定义方法“user_path”

我的代码是:

活动控制器.rb

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

我的活动 index.html.erb 文件

<h1>feeds</h1>
<% @activities.each do |activity| %>
  <div class="activity">
    <%= link_to activity.owner.full_name, activity.owner if activity.owner %>
    added comment to <%= link_to activity.trackable.shipment.name.activity.trackable.shipment %>
    </div>
    <% end %>

我的comment.rb 文件

class Comment < ActiveRecord::Base
include PublicActivity::Model
 tracked owner: ->(controller, model) { controller && controller.current_user }
  belongs_to :shipment
  belongs_to :user
end

我的 routes.rb 文件

Rails.application.routes.draw do

  get 'activities/index'

  get 'profiles/show'
  get 'pages/homepage'


  devise_for :users, :controllers => {:registrations => "registrations"}
  resources :shipments do
    member do
      get "like", to: "shipments#upvote"
    end
    resources :comments
  end
  # The priority is based upon order of creation: first created -> highest priority.
  # See how all your routes lay out with "rake routes".
  root "shipments#index"
  get '/:id', to: 'profiles#show'



  get "mailbox/inbox" => "mailbox#inbox", as: :mailbox_inbox
  get "mailbox/sent" => "mailbox#sent", as: :mailbox_sent
  get "mailbox/trash" => "mailbox#trash", as: :mailbox_trash
    resources :conversations do
    member do
      post :reply
      post :trash
      post :untrash
      resources :users
    end
  end
end

堆栈跟踪

  Rendered activities/index.html.erb within layouts/application (31.2ms)
Completed 500 Internal Server Error in 109ms (ActiveRecord: 78.0ms)

ActionView::Template::Error (undefined method `shipment' for #<Shipment:0x4dc44b
0>):
    2: <% @activities.each do |activity| %>
    3:   <div class="activity">
    4:     <%= link_to activity.owner.profile_name, activity.owner if activity.o
wner %>
    5:     added comment to <%= link_to activity.trackable.shipment.name, activi
ty.trackable.shipment %>
    6: <% end %>
  app/views/activities/index.html.erb:5:in `block in _app_views_activities_index
_html_erb___51428318_40366872'
  app/views/activities/index.html.erb:2:in `_app_views_activities_index_html_erb
___51428318_40366872'
4

1 回答 1

1

Ryan 在 Railscast 中没有提到的是,您可能会获得属于不同类型的可跟踪对象的活动。

因此,如果您已为发货模型设置跟踪:

class Shipment < ActiveRecord::Base
  include PublicActivity::Model
  tracked
  # ...
  belongs_to :user
  has_many :comments, dependent: :destroy
end

并创建了一些发货记录和一些推荐,而不是您的活动表将包含类似这样的内容

id  trackable_type # ...
1   Shipment
2   Shipment
3   Comment

以下是两种可能的解决方案:

1. 过滤一种可追踪类型的查询。

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

2. 对每种类型的可追踪对象使用不同的部分:

<% @activities.each do |activity| %>
  <%= render partial "activities/#{ activity.trackable.model_name.singular }", 
     activity: activity, 
     object: activity.trackable,
     owner: activity.owner
  %>
<% end %>

这样做是渲染app/views/activities/_comment.html.erb_shipment.html.erb取决于类型。我们还设置了一些局部变量,这样我们就不必编写activity.foo.bar.baz.

我们还使用object创建与部分同名的实例变量(@ 变量)的选项。所以它会@shipment_shipment.html.erb.

所以在你的_comment.html.erb你会做:

<div class="activity">
  <%= link_to owner.full_name, owner if owner %>
  added comment to <%= link_to @comment.shipment.name, @comment.shipment %>
</div>

也可以看看:

于 2015-10-09T14:05:37.093 回答