Why do I keep getting a routing error when visiting a users page in my Ruby on Rails application?

自闭症网瘾萝莉.ら 提交于 2019-12-02 10:39:47

问题


I am trying to add comments to the microposts found in https://github.com/railstutorial/sample_app_2nd_ed from the michael hartl railstutorial.

It seems that if a user has made no microposts, there is no problem and I can access their page. However if they have there is a routing error. This is my problem.

Here is the area in question on my routes.rb file.

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

This is the error I get when I try to visit a users page (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=>#<Micropost id: nil, content: nil, user_id: nil, created_at: nil, updated_at: nil>}):
  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)

here is output of 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)  

and here is my 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

来源:https://stackoverflow.com/questions/10661569/why-do-i-keep-getting-a-routing-error-when-visiting-a-users-page-in-my-ruby-on-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!