0

I have a simple rails app and i am trying to add an activity feed on it. For that i am using the Public Activity Gem and was following the this Railscast

but now it's giving me an error:

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

My codes are:

activities_controller.rb

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

My Activity index.html.erb file

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

My comment.rb file

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

My routes.rb file

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

So if anyone can help how to get out of this. Thanks in advance.

stack trace

  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)

Routes on terminal

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
Ahmed Reza
  • 293
  • 1
  • 3
  • 19

1 Answers1

0

The problem is with this statement:

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

activity.owner is a User object and will generate a user_path link (typically /user/<owner_id>)

I think you need a

resources :users

line in your config.rb (which should probably be wrapped in a authenticated :user do block)

More information:

The link_to call generates a link to the activity owner's page.

You need to have a controller serve a page showing the owner's details. Since the owner is a User model (you wrote devise_for :users), the activity.owner is a User object.

When link_to sees the User object it tries to call user_path(owner.id) to generate the HTTP URL for that user. For this to work, you need such a route in your config.rb file.

When you add a resources :user line to your config.routes and a corresponding UsersController (and view) you create an HTTP endpoint for showing/creating/editing users.

I suggest you review the railscast explaining Devise.

nimrodm
  • 23,081
  • 7
  • 58
  • 59
  • SIR @nimrodm can you please explain exactly what i have to do...i am new to rails – Ahmed Reza Oct 04 '15 at 10:26
  • @Denialsmith - tried to add more information. You should really review the railscast that deals with devise. – nimrodm Oct 04 '15 at 10:36
  • hey @nimrodm i did as u said but now it's giving undefined method `activity' for "tittle":String – Ahmed Reza Oct 04 '15 at 18:13
  • @Denialsmith, I guess this is further along the Activity index.html.erb file? The error no longer appears at the 'link_to' line, right? If so, please update and show the relevant part of (or all of) Activity index.html.erb. – nimrodm Oct 05 '15 at 04:20