未定义的方法错误'公共活动宝石'

时间:2015-10-07 14:07:52

标签: ruby-on-rails activerecord railscasts public-activity

我有一个简单的rails应用程序,我正在尝试添加一个活动源。为此,我正在使用公共活动宝石并遵循此Railscast

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

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

我的代码是:

activities_controller.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 %>

我的评论.rb文件

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

我有一个简单的rails应用程序,我正在尝试添加一个活动源。为此,我正在使用公共活动宝石,并遵循这个Railscast

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

#&lt;#:0x4a90a30&gt;

的未定义方法`user_path'

我的代码是:

activities_controller.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 %>

我的评论.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'

1 个答案:

答案 0 :(得分: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选项创建一个与partial相同名称的实例变量(@变量)。因此,@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>

另见: