我仍在学习Ruby on rails,现在我被困在public_activity gem。我肯定知道的是,当我检查实例变量@activities
时,gem正在工作。
活动视图:
<% @activities.each do |activity| %>
<%= activity.inspect %>
<% end %>
活动控制器:
class ActivitiesController < ApplicationController
def index
@activities = PublicActivity::Activity.order("created_at desc")
end
end
结果:
#<PublicActivity::Activity id: 2, trackable_id: 7, trackable_type: "Post", owner_id: 1, owner_type: "User", key: "post.create", parameters: {}, recipient_id: nil, recipient_type: nil, created_at: "2016-02-19 11:20:46", updated_at: "2016-02-19 11:20:46"> #<PublicActivity::Activity id: 1, trackable_id: 6, trackable_type: "Post", owner_id: nil, owner_type: nil, key: "post.create", parameters: {}, recipient_id: nil, recipient_type: nil, created_at: "2016-02-19 11:16:08", updated_at: "2016-02-19 11:16:08">
现在我正在尝试实际显示用户决定关注某人的活动。 (使用acts_as_follower)
活动视图:
<% @activities.each do |activity| %>
<%= link_to activity.owner.email, activity.owner if activity.owner %>
<% end %>
未定义方法user_path
出现时的情况。这是我的路线吗?
Routes.rb:
Rails.application.routes.draw do
resources :posts
resources :activities
devise_for :users
root 'page#index'
get '/users/:id' => 'page#profile'
get 'page/feed'
get 'page/home'
resources :page do
member do
get :follow
get :unfollow
end
end
end
发布模型:
class Post < ActiveRecord::Base
include PublicActivity::Model
tracked owner: Proc.new{ |controller, model| controller.current_user }
belongs_to :user
mount_uploader :avatar, AvatarUploader
end
注意:我使用devise作为我的用户身份验证
应用程序控制器:
class ApplicationController < ActionController::Base
include PublicActivity::StoreController
protect_from_forgery with: :exception
end
耙路线:
Prefix Verb URI Pattern Controller#Action
posts GET /posts(.:format) posts#index
POST /posts(.:format) posts#create
new_post GET /posts/new(.:format) posts#new
edit_post GET /posts/:id/edit(.:format) posts#edit
post GET /posts/:id(.:format) posts#show
PATCH /posts/:id(.:format) posts#update
PUT /posts/:id(.:format) posts#update
DELETE /posts/:id(.:format) posts#destroy
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
PATCH /activities/:id(.:format) activities#update
PUT /activities/:id(.:format) activities#update
DELETE /activities/:id(.:format) activities#destro
y
new_user_session GET /users/sign_in(.:format) devise/sessions#n
ew
user_session POST /users/sign_in(.:format) devise/sessions#c
reate
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#d
estroy
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) devise/registrati
ons#cancel
user_registration POST /users(.:format) devise/registrati
ons#create
new_user_registration GET /users/sign_up(.:format) devise/registrati
ons#new
edit_user_registration GET /users/edit(.:format) devise/registrati
ons#edit
PATCH /users(.:format) devise/registrati
ons#update
PUT /users(.:format) devise/registrati
ons#update
DELETE /users(.:format) devise/registrati
ons#destroy
root GET / page#index
GET /users/:id(.:format) page#profile
page_feed GET /page/feed(.:format) page#feed
page_home GET /page/home(.:format) page#home
follow_page GET /page/:id/follow(.:format) page#follow
unfollow_page GET /page/:id/unfollow(.:format) page#unfollow
page_index GET /page(.:format) page#index
POST /page(.:format) page#create
new_page GET /page/new(.:format) page#new
edit_page GET /page/:id/edit(.:format) page#edit
page GET /page/:id(.:format) page#show
PATCH /page/:id(.:format) page#update
PUT /page/:id(.:format) page#update
DELETE /page/:id(.:format) page#destroy
完整追踪:
actionpack (4.1.8) lib/action_dispatch/routing/polymorphic_routes.rb:142:in `polymorphic_url'
actionpack (4.1.8) lib/action_dispatch/routing/polymorphic_routes.rb:148:in `polymorphic_path'
actionview (4.1.8) lib/action_view/routing_url_for.rb:89:in `url_for'
turbolinks (2.5.3) lib/turbolinks/xhr_url_for.rb:12:in `url_for_with_xhr_referer'
actionview (4.1.8) lib/action_view/helpers/url_helper.rb:181:in `link_to'
app/views/activities/index.html.erb:4:in `block in _app_views_activities_index_html_erb__460545569_51337044'
C:in `each'
C:in `each'
app/views/activities/index.html.erb:3:in `_app_views_activities_index_html_erb__460545569_51337044'
actionview (4.1.8) lib/action_view/template.rb:145:in `block in render'
activesupport (4.1.8) lib/active_support/notifications.rb:161:in `instrument'
actionview (4.1.8) lib/action_view/template.rb:339:in `instrument'
actionview (4.1.8) lib/action_view/template.rb:143:in `render'
actionview (4.1.8) lib/action_view/renderer/template_renderer.rb:55:in `block (2 levels) in render_template'
actionview (4.1.8) lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument'
activesupport (4.1.8) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.1.8) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.1.8) lib/active_support/notifications.rb:159:in `instrument'
actionview (4.1.8) lib/action_view/renderer/abstract_renderer.rb:38:in `instrument'
actionview (4.1.8) lib/action_view/renderer/template_renderer.rb:54:in `block in render_template'
actionview (4.1.8) lib/action_view/renderer/template_renderer.rb:62:in `render_with_layout'
actionview (4.1.8) lib/action_view/renderer/template_renderer.rb:53:in `render_template'
actionview (4.1.8) lib/action_view/renderer/template_renderer.rb:17:in `render'
actionview (4.1.8) lib/action_view/renderer/renderer.rb:42:in `render_template'
actionview (4.1.8) lib/action_view/renderer/renderer.rb:23:in `render'
actionview (4.1.8) lib/action_view/rendering.rb:99:in `_render_template'
actionpack (4.1.8) lib/action_controller/metal/streaming.rb:217:in `_render_template'
actionview (4.1.8) lib/action_view/rendering.rb:82:in `render_to_body'
actionpack (4.1.8) lib/action_controller/metal/rendering.rb:32:in `render_to_body'
actionpack (4.1.8) lib/action_controller/metal/renderers.rb:32:in `render_to_body'
actionpack (4.1.8) lib/abstract_controller/rendering.rb:25:in `render'
actionpack (4.1.8) lib/action_controller/metal/rendering.rb:16:in `render'
actionpack (4.1.8) lib/action_controller/metal/instrumentation.rb:41:in `block (2 levels) in render'
activesupport (4.1.8) lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/benchmark.rb:294:in `realtime'
activesupport (4.1.8) lib/active_support/core_ext/benchmark.rb:12:in `ms'
actionpack (4.1.8) lib/action_controller/metal/instrumentation.rb:41:in `block in render'
actionpack (4.1.8) lib/action_controller/metal/instrumentation.rb:84:in `cleanup_view_runtime'
activerecord (4.1.8) lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime'
actionpack (4.1.8) lib/action_controller/metal/instrumentation.rb:40:in `render'
actionpack (4.1.8) lib/action_controller/metal/implicit_render.rb:10:in `default_render'
actionpack (4.1.8) lib/action_controller/metal/implicit_render.rb:5:in `send_action'
actionpack (4.1.8) lib/abstract_controller/base.rb:189:in `process_action'
actionpack (4.1.8) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.1.8) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (4.1.8) lib/active_support/callbacks.rb:113:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:113:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:229:in `block in halting'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.8) lib/active_support/callbacks.rb:229:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:229:in `block in halting'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.8) lib/active_support/callbacks.rb:86:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:86:in `run_callbacks'
actionpack (4.1.8) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.1.8) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.1.8) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
activesupport (4.1.8) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.1.8) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.1.8) lib/active_support/notifications.rb:159:in `instrument'
actionpack (4.1.8) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.1.8) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
activerecord (4.1.8) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.1.8) lib/abstract_controller/base.rb:136:in `process'
actionview (4.1.8) lib/action_view/rendering.rb:30:in `process'
actionpack (4.1.8) lib/action_controller/metal.rb:196:in `dispatch'
actionpack (4.1.8) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.1.8) lib/action_controller/metal.rb:232:in `block in action'
actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:82:in `call'
actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:82:in `dispatch'
actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:50:in `call'
actionpack (4.1.8) lib/action_dispatch/journey/router.rb:73:in `block in call'
actionpack (4.1.8) lib/action_dispatch/journey/router.rb:59:in `each'
actionpack (4.1.8) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:678:in `call'
warden (1.2.6) lib/warden/manager.rb:35:in `block in call'
warden (1.2.6) lib/warden/manager.rb:34:in `catch'
warden (1.2.6) lib/warden/manager.rb:34:in `call'
rack (1.5.5) lib/rack/etag.rb:23:in `call'
rack (1.5.5) lib/rack/conditionalget.rb:25:in `call'
rack (1.5.5) lib/rack/head.rb:11:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/flash.rb:254:in `call'
rack (1.5.5) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.5.5) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.1.8) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.1.8) lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in `call'
activerecord (4.1.8) lib/active_record/migration.rb:380:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.1.8) lib/active_support/callbacks.rb:82:in `run_callbacks'
actionpack (4.1.8) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.1.8) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.1.8) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.1.8) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.1.8) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.1.8) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.1.8) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.5) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.5) lib/rack/runtime.rb:17:in `call'
activesupport (4.1.8) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
rack (1.5.5) lib/rack/lock.rb:17:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/static.rb:84:in `call'
rack (1.5.5) lib/rack/sendfile.rb:112:in `call'
railties (4.1.8) lib/rails/engine.rb:514:in `call'
railties (4.1.8) lib/rails/application.rb:144:in `call'
rack (1.5.5) lib/rack/lock.rb:17:in `call'
rack (1.5.5) lib/rack/content_length.rb:14:in `call'
rack (1.5.5) lib/rack/handler/webrick.rb:60:in `service'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'
答案 0 :(得分:1)
实际上问题来自您的routes.rb
文件。
您的观点实际上是在搜索user_path
,因为activity.owner
是User
。
为了解决问题,您应该在路径文件中添加资源:
Rails.application.routes.draw do
resources :posts
resources :activities
resources :user
devise_for :users
root 'page#index'
get '/users/:id' => 'page#profile'
get 'page/feed'
get 'page/home'
resources :page do
member do
get :follow
get :unfollow
end
end
end
我希望这会有所帮助!
编辑:
我的不好,错过了get '/users/:id' => 'page#profile'
这为你提供了这个输出:
root GET / page#index
GET /users/:id(.:format) page#profile
所以你应该在视图中手动设置路径,如下所示:
<% @activities.each do |activity| %>
<%= link_to activity.owner.email, root_path(activity.owner_id) if activity.owner %>
<% end %>
但这仍然不是很好。最好在routes.rb
文件中创建资源,并为您的用户模型设置正确的路径。