在Ruby on Rails应用程序中访问用户页面时,为什么一直出现路由错误?

我试图在michael hartl railstutorial的https://github.com/railstutorial/sample_app_2nd_ed中找到微博的注释。

似乎如果用户没有制作微博,则没有问题,我可以访问他们的页面。 但是,如果他们有路由错误。 这是我的问题。

这是我的routes.rb文件中的问题区域。

resources :microposts, only: [:create, :destroy] do resources :comments, end 

这是我尝试访问用户页面时遇到的错误(localhost:3000 / users / 2):

 Started GET "/users/2" for 127.0.0.1 at 2012-05-21 21:32:47 -0700 Processing by UsersController#show as HTML Parameters: {"id"=>"2"} User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", "2"]] User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."remember_token" = 'iGivWVhrOQL2tp1MOOJwgA' LIMIT 1 Rendered users/_follow_form.html.erb (0.8ms) (0.2ms) SELECT COUNT(*) FROM "microposts" WHERE "microposts"."user_id" = 2 CACHE (0.0ms) SELECT COUNT(*) FROM "microposts" WHERE "microposts"."user_id" = 2 Micropost Load (0.1ms) SELECT "microposts".* FROM "microposts" WHERE "microposts"."user_id" = 2 ORDER BY microposts.created_at DESC LIMIT 30 OFFSET 0 User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 2 LIMIT 1 Rendered microposts/_micropost.html.erb (12.2ms) Rendered users/show.html.erb within layouts/application (53.1ms) Completed 500 Internal Server Error in 69ms ActionController::RoutingError (No route matches {:controller=>"comments", :format=>nil, :micropost_id=>#}): app/views/microposts/_micropost.html.erb:23:in `_app_views_microposts__micropost_html_erb___1795431923022101738_32372620' app/views/users/show.html.erb:16:in `_app_views_users_show_html_erb__1221738286899740817_32260600' Rendered /home/alex/.rvm/gems/ruby-1.9.3-p125@rails3tutorial2ndEd/gems/actionpack-3.2.3/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (0.6ms) 

这里是rake grep | comments的输出

 user_comments GET /users/:user_id/comments(.:format) comments#index POST /users/:user_id/comments(.:format) comments#create new_user_comment GET /users/:user_id/comments/new(.:format) comments#new edit_user_comment GET /users/:user_id/comments/:id/edit(.:format) comments#edit user_comment GET /users/:user_id/comments/:id(.:format) comments#show PUT /users/:user_id/comments/:id(.:format) comments#update DELETE /users/:user_id/comments/:id(.:format) comments#destroy micropost_comments GET /microposts/:micropost_id/comments(.:format) comments#index POST /microposts/:micropost_id/comments(.:format) comments#create new_micropost_comment GET /microposts/:micropost_id/comments/new(.:format) comments#new edit_micropost_comment GET /microposts/:micropost_id/comments/:id/edit(.:format) comments#edit micropost_comment GET /microposts/:micropost_id/comments/:id(.:format) comments#show PUT /microposts/:micropost_id/comments/:id(.:format) comments#update DELETE /microposts/:micropost_id/comments/:id(.:format) 

这是我的comments_controller.rb

 class CommentsController < ApplicationController def create @micropost = Micropost.find(params[:micropost_id]) @comment = @micropost.comments.build(params[:comment]) @comment.user = current_user if @comment.save redirect_to @micropost else redirect_to @micropost end end def show @comment = Comment.find(params[:id]) end def new end def destroy @comment = Comment.find(params[:id]) @comment.destroy redirect_back_or root_path end end