public_activity rails undefined方法user_path

时间:2013-03-07 08:39:58

标签: ruby-on-rails ruby-on-rails-3

我在将public_activity添加到我的rails应用程序时进展缓慢。

它正在运行,但我在调用正确的方法和链接时遇到问题,使其看起来很漂亮

当我的activities.index.html只有

<% @activities.each do |activity| %>
    <%= activity.inspect %>
<% end %>

在我看来,我可以看到

#<PublicActivity::ORM::ActiveRecord::Activity id: 2, trackable_id: 41, trackable_type: "Guideline", owner_id: 12, owner_type: "User", key: "guideline.create", parameters: {}, recipient_id: nil, recipient_type: nil, created_at: "2013-03-07 08:25:45", updated_at: "2013-03-07 08:25:45"> #<PublicActivity::ORM::ActiveRecord::Activity id: 1, trackable_id: 40, trackable_type: "Guideline", owner_id: nil, owner_type: nil, key: "guideline.create", parameters: {}, recipient_id: nil, recipient_type: nil, created_at: "2013-03-07 08:21:09", updated_at: "2013-03-07 08:21:09">

当我的activities/index.html视图

<% @activities.each do |activity| %>
    <div class="activity">
    <%= link_to activity.owner.profile_name, activity.owner if activity.owner %>
    added guideline to <%= link_to activity.trackable.guideline.title, activity.trackable.guideline %>
</div>
<% end %>

错误是

undefined method `user_path'

这与我的路线有关吗? 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'

end

运行rake路线给了我

activities_index GET        /activities/index(.:format)                           activities#index
                    admin_root            /admin(.:format)                                      admin/dashboard#index
batch_action_admin_admin_users POST       /admin/admin_users/batch_action(.:format)             admin/admin_users#batch_action
             admin_admin_users GET        /admin/admin_users(.:format)                          admin/admin_users#index
                               POST       /admin/admin_users(.:format)                          admin/admin_users#create
          new_admin_admin_user GET        /admin/admin_users/new(.:format)                      admin/admin_users#new
         edit_admin_admin_user GET        /admin/admin_users/:id/edit(.:format)                 admin/admin_users#edit
              admin_admin_user GET        /admin/admin_users/:id(.:format)                      admin/admin_users#show
                               PUT        /admin/admin_users/:id(.:format)                      admin/admin_users#update
                               DELETE     /admin/admin_users/:id(.:format)                      admin/admin_users#destroy
               admin_dashboard            /admin/dashboard(.:format)                            admin/dashboard#index
 batch_action_admin_guidelines POST       /admin/guidelines/batch_action(.:format)              admin/guidelines#batch_action
              admin_guidelines GET        /admin/guidelines(.:format)                           admin/guidelines#index
                               POST       /admin/guidelines(.:format)                           admin/guidelines#create
           new_admin_guideline GET        /admin/guidelines/new(.:format)                       admin/guidelines#new
          edit_admin_guideline GET        /admin/guidelines/:id/edit(.:format)                  admin/guidelines#edit
               admin_guideline GET        /admin/guidelines/:id(.:format)                       admin/guidelines#show
                               PUT        /admin/guidelines/:id(.:format)                       admin/guidelines#update
                               DELETE     /admin/guidelines/:id(.:format)                       admin/guidelines#destroy
      batch_action_admin_users POST       /admin/users/batch_action(.:format)                   admin/users#batch_action
                   admin_users GET        /admin/users(.:format)                                admin/users#index
                               POST       /admin/users(.:format)                                admin/users#create
                new_admin_user GET        /admin/users/new(.:format)                            admin/users#new
               edit_admin_user GET        /admin/users/:id/edit(.:format)                       admin/users#edit
                    admin_user GET        /admin/users/:id(.:format)                            admin/users#show
                               PUT        /admin/users/:id(.:format)                            admin/users#update
                               DELETE     /admin/users/:id(.:format)                            admin/users#destroy
   batch_action_admin_comments POST       /admin/comments/batch_action(.:format)                admin/comments#batch_action
                admin_comments GET        /admin/comments(.:format)                             admin/comments#index
                               POST       /admin/comments(.:format)                             admin/comments#create
                 admin_comment GET        /admin/comments/:id(.:format)                         admin/comments#show
        new_admin_user_session GET        /admin/login(.:format)                                active_admin/devise/sessions#new
            admin_user_session POST       /admin/login(.:format)                                active_admin/devise/sessions#create
    destroy_admin_user_session DELETE|GET /admin/logout(.:format)                               active_admin/devise/sessions#destroy
           admin_user_password POST       /admin/password(.:format)                             active_admin/devise/passwords#create
       new_admin_user_password GET        /admin/password/new(.:format)                         active_admin/devise/passwords#new
      edit_admin_user_password GET        /admin/password/edit(.:format)                        active_admin/devise/passwords#edit
                               PUT        /admin/password(.:format)                             active_admin/devise/passwords#update
              guidelines_topic GET        /guidelines/topic(.:format)                           guidelines#topic
      guidelines_topichospital GET        /guidelines/topichospital(.:format)                   guidelines#topichospital
     guidelines_topicspecialty GET        /guidelines/topicspecialty(.:format)                  guidelines#topicspecialty
          guidelines_favourite GET        /guidelines/favourite(.:format)                       guidelines#favourite
                 profiles_show GET        /profiles/show(.:format)                              profiles#show
               guidelines_show GET        /guidelines/show(.:format)                            guidelines#show
              new_user_session GET        /users/sign_in(.:format)                              devise/sessions#new
                  user_session POST       /users/sign_in(.:format)                              devise/sessions#create
          destroy_user_session DELETE     /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
                               PUT        /users/password(.:format)                             devise/passwords#update
      cancel_user_registration GET        /users/cancel(.:format)                               devise/registrations#cancel
             user_registration POST       /users(.:format)                                      devise/registrations#create
         new_user_registration GET        /users/sign_up(.:format)                              devise/registrations#new
        edit_user_registration GET        /users/edit(.:format)                                 devise/registrations#edit
                               PUT        /users(.:format)                                      devise/registrations#update
                               DELETE     /users(.:format)                                      devise/registrations#destroy
                      register GET        /signup(.:format)                                     devise/registrations#new
                         login GET        /login(.:format)                                      devise/sessions#new
                        logout GET        /logout(.:format)                                     devise/sessions#destroy
                          edit GET        /edit(.:format)                                       devise/registrations#edit
             user_registration PUT        /users(.:format)                                      devise/registrations#update
                         about GET        /about(.:format)                                      about#about
                    guidelines GET        /guidelines(.:format)                                 guidelines#index
                               POST       /guidelines(.:format)                                 guidelines#create
                 new_guideline GET        /guidelines/new(.:format)                             guidelines#new
                edit_guideline GET        /guidelines/:id/edit(.:format)                        guidelines#edit
                     guideline GET        /guidelines/:id(.:format)                             guidelines#show
                               PUT        /guidelines/:id(.:format)                             guidelines#update
                               DELETE     /guidelines/:id(.:format)                             guidelines#destroy
                    guidelines GET        /guidelines(.:format)                                 guidelines#index
                    favourites GET        /favourites(.:format)                                 favourites#show
                        topics GET        /topics(.:format)                                     guidelines#list
                     hospitals GET        /hospitals(.:format)                                  guidelines#listhospital
                   specialties GET        /specialties(.:format)                                guidelines#listspecialty
                    activities GET        /activities(.:format)                                 activities#index
                               POST       /activities(.:format)                                 activities#create
                  new_activity GET        /activities/new(.:format)                             activities#new
                 edit_activity GET        /activities/:id/edit(.:format)                        activities#edit
                      activity GET        /activities/:id(.:format)                             activities#show
                               PUT        /activities/:id(.:format)                             activities#update
                               DELETE     /activities/:id(.:format)                             activities#destroy
                          root            /                                                     guidelines#index
            guideline_comments GET        /guidelines/:guideline_id/comments(.:format)          comments#index
                               POST       /guidelines/:guideline_id/comments(.:format)          comments#create
         new_guideline_comment GET        /guidelines/:guideline_id/comments/new(.:format)      comments#new
        edit_guideline_comment GET        /guidelines/:guideline_id/comments/:id/edit(.:format) comments#edit
             guideline_comment GET        /guidelines/:guideline_id/comments/:id(.:format)      comments#show
                               PUT        /guidelines/:guideline_id/comments/:id(.:format)      comments#update
                               DELETE     /guidelines/:guideline_id/comments/:id(.:format)      comments#destroy
                               GET        /guidelines(.:format)                                 guidelines#index
                               POST       /guidelines(.:format)                                 guidelines#create
                               GET        /guidelines/new(.:format)                             guidelines#new
                               GET        /guidelines/:id/edit(.:format)                        guidelines#edit
                               GET        /guidelines/:id(.:format)                             guidelines#show
                               PUT        /guidelines/:id(.:format)                             guidelines#update
                               DELETE     /guidelines/:id(.:format)                             guidelines#destroy

2 个答案:

答案 0 :(得分:1)

您将获得未定义的方法user_path,因为路由文件不包含用户show view的路由。你必须创建一个才能工作。首先,您应该创建一个users_controller并在其中定义show方法。

class UsersController << ApplicationController   
  def show
    @user = User.find(params[:id])
  end
end

然后,在您的routes.rb文件中,您将要制作路线。

match '/users/:id', :to => 'users#show', :as => :user

:as参数告诉路由器将路由命名为什么(然后可以将_path或_url添加到:as参数中)。您想要调用此用户,因为当您直接链接到ActiveRecord模型(如用户)时,Rails会尝试将该模型转换为model_path。

答案 1 :(得分:0)

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

表示activity.owner属于user类型,并链接到user_path(activity.owner)

尝试做一个rake路线。并查看用户#show是否已定义。