0

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

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

undefined method `user_path' for #<#<Class:0x4a8b288>:0x4a90a30>

我的代码是:

活动控制器.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 '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 'activities/index'



  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
    end
  end
end

因此,如果有人可以帮助如何摆脱这种情况。提前致谢。

堆栈跟踪

  Rendered activities/index.html.erb within layouts/application (122.0ms)
Completed 500 Internal Server Error in 306ms (ActiveRecord: 155.0ms)

ActionView::Template::Error (undefined method `user_path' for #<#<Class:0x4b8158
8>:0x4b88560>):
    1: <h1>fgfgh</h1>
    2: <% @activities.each do |activity| %>
    3:   <div class="activity">
    4:     <%= link_to activity.owner.full_name, activity.owner if activity.owne
r %>
    5:     added comment to <%= link_to activity.trackable.shipment.name.activit
y.trackable.shipment %>
    6:     </div>
    7:  <% end %>
  app/views/activities/index.html.erb:4:in `block in _app_views_activities_index
_html_erb__319514432_39684012'
  app/views/activities/index.html.erb:2:in `_app_views_activities_index_html_erb
__319514432_39684012'


  Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.2.3
/lib/action_dispatch/middleware/templates/rescues/_source.erb (2.0ms)
  Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.2.3
/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (5.0ms)
  Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.2.3
/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
 (1.0ms)
  Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.2.3
/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb within
 rescues/layout (144.0ms)
  Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/web-console-2.2.
1/lib/web_console/templates/_markup.html.erb (1.0ms)
  Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/web-console-2.2.
1/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inline
d_string (0.0ms)
  Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/web-console-2.2.
1/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_s
tring (1.0ms)
  Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/web-console-2.2.
1/lib/web_console/templates/style.css.erb within layouts/inlined_string (1.0ms)
  Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/web-console-2.2.
1/lib/web_console/templates/console.js.erb within layouts/javascript (83.0ms)
  Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/web-console-2.2.
1/lib/web_console/templates/main.js.erb within layouts/javascript (0.0ms)
  Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/web-console-2.2.
1/lib/web_console/templates/error_page.js.erb within layouts/javascript (1.0ms)
  Rendered C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/web-console-2.2.
1/lib/web_console/templates/index.html.erb (203.0ms)

航站楼路线

DL is deprecated, please use Fiddle
                  Prefix Verb   URI Pattern
    Controller#Action
           profiles_show GET    /profiles/show(.:format)
    profiles#show
          pages_homepage GET    /pages/homepage(.:format)
    pages#homepage
        new_user_session GET    /users/sign_in(.:format)
    devise/sessions#new
            user_session POST   /users/sign_in(.:format)
    devise/sessions#create
    destroy_user_session GET    /users/sign_out(.:format)
    devise/sessions#destroy
           user_password POST   /users/password(.:format)
    devise/passwords#create
       new_user_password GET    /users/password/new(.:format)
    devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format)
    devise/passwords#edit
                         PATCH  /users/password(.:format)
    devise/passwords#update
                         PUT    /users/password(.:format)
    devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)
    registrations#cancel
       user_registration POST   /users(.:format)
    registrations#create
   new_user_registration GET    /users/sign_up(.:format)
    registrations#new
  edit_user_registration GET    /users/edit(.:format)
    registrations#edit
                         PATCH  /users(.:format)
    registrations#update
                         PUT    /users(.:format)
    registrations#update
                         DELETE /users(.:format)
    registrations#destroy
           like_shipment GET    /shipments/:id/like(.:format)
    shipments#upvote
       shipment_comments GET    /shipments/:shipment_id/comments(.:format)
    comments#index
                         POST   /shipments/:shipment_id/comments(.:format)
    comments#create
    new_shipment_comment GET    /shipments/:shipment_id/comments/new(.:format)
    comments#new
   edit_shipment_comment GET    /shipments/:shipment_id/comments/:id/edit(.:form
at) comments#edit
        shipment_comment GET    /shipments/:shipment_id/comments/:id(.:format)
    comments#show
                         PATCH  /shipments/:shipment_id/comments/:id(.:format)
    comments#update
                         PUT    /shipments/:shipment_id/comments/:id(.:format)
    comments#update
                         DELETE /shipments/:shipment_id/comments/:id(.:format)
    comments#destroy
               shipments GET    /shipments(.:format)
    shipments#index
                         POST   /shipments(.:format)
    shipments#create
            new_shipment GET    /shipments/new(.:format)
    shipments#new
           edit_shipment GET    /shipments/:id/edit(.:format)
    shipments#edit
                shipment GET    /shipments/:id(.:format)
    shipments#show
                         PATCH  /shipments/:id(.:format)
    shipments#update
                         PUT    /shipments/:id(.:format)
    shipments#update
                         DELETE /shipments/:id(.:format)
    shipments#destroy
                    root GET    /
    shipments#index
                         GET    /:id(.:format)
    profiles#show
        activities_index GET    /activities/index(.:format)
    activities#index
           mailbox_inbox GET    /mailbox/inbox(.:format)
    mailbox#inbox
            mailbox_sent GET    /mailbox/sent(.:format)
    mailbox#sent
           mailbox_trash GET    /mailbox/trash(.:format)
    mailbox#trash
      reply_conversation POST   /conversations/:id/reply(.:format)
    conversations#reply
      trash_conversation POST   /conversations/:id/trash(.:format)
    conversations#trash
    untrash_conversation POST   /conversations/:id/untrash(.:format)
    conversations#untrash
           conversations GET    /conversations(.:format)
    conversations#index
                         POST   /conversations(.:format)
    conversations#create
        new_conversation GET    /conversations/new(.:format)
    conversations#new
       edit_conversation GET    /conversations/:id/edit(.:format)
    conversations#edit
            conversation GET    /conversations/:id(.:format)
    conversations#show
                         PATCH  /conversations/:id(.:format)
    conversations#update
                         PUT    /conversations/:id(.:format)
    conversations#update
                         DELETE /conversations/:id(.:format)
    conversations#destroy
4

1 回答 1

0

问题出在这句话上:

<%= link_to activity.owner.full_name, activity.owner if activity.owner %>

activity.owner是一个User对象,将生成一个user_path链接(通常/user/<owner_id>

我认为你需要一个

resources :users

你的行config.rb(可能应该被包裹在一个 authenticated :user do块中)

更多信息:

link_to调用会生成指向活动所有者页面的链接。

您需要让控制器提供显示所有者详细信息的页面。由于所有者是一个User模型(你写了devise_for :users),所以activity.owner它是一个User对象。

link_to看到User对象时,它会尝试调用user_path(owner.id)以生成该用户的 HTTP URL。为此,您需要在 config.rb 文件中添加这样的路由。

当您向您的和相应的(和视图)添加resources :user一行时,您将创建一个 HTTP 端点以显示/创建/编辑用户。config.routesUsersController

我建议您查看解释设计的 railscast。

于 2015-10-04T09:51:59.000 回答