Search code examples
ruby-on-railsruby-on-rails-4rails-routing

Why the following error?


I am building a nested resource like

 resources :blogs do
    resources :comments
  end

when I visit the following

blogs/1/comments/2, I received the error

undefined method `comment_url' for #<#<Class:0x4173108>:0x43b45d8>

Here is the code inside views\comments_comment.json.jbuilder

json.url comment_url(comment, format: :json)

How to fix?

Edit per suggestion

json.url blog_comments_url(comment.blog, comment, format: :json)

However the URl I get is always like "url":"http://localhost:3000/blogs/1/comments.json"

I would like to get something like "url":"http://localhost:3000/blogs/1/comments/2.json"


Solution

  • If you do a rake routes, you should see something like this:

        blog_comments GET    /blogs/:blog_id/comments(.:format)          comments#index                                     
                      POST   /blogs/:blog_id/comments(.:format)          comments#create                                    
     new_blog_comment GET    /blogs/:blog_id/comments/new(.:format)      comments#new                                       
    edit_blog_comment GET    /blogs/:blog_id/comments/:id/edit(.:format) comments#edit                                      
         blog_comment GET    /blogs/:blog_id/comments/:id(.:format)      comments#show                                      
                      PATCH  /blogs/:blog_id/comments/:id(.:format)      comments#update                                    
                      PUT    /blogs/:blog_id/comments/:id(.:format)      comments#update                                    
                      DELETE /blogs/:blog_id/comments/:id(.:format)      comments#destroy                                   
                blogs GET    /blogs(.:format)                            blogs#index                                        
                      POST   /blogs(.:format)                            blogs#create                                       
             new_blog GET    /blogs/new(.:format)                        blogs#new                                          
            edit_blog GET    /blogs/:id/edit(.:format)                   blogs#edit                                         
                 blog GET    /blogs/:id(.:format)                        blogs#show                                         
                      PATCH  /blogs/:id(.:format)                        blogs#update                                       
                      PUT    /blogs/:id(.:format)                        blogs#update                                       
                      DELETE /blogs/:id(.:format)                        blogs#destroy                                      
    

    The first column has the path helper name without the _url or _path suffix.

    Your :comments resources is nested within your :blogs resource so the helper names are also nested:

    • blog_comments not comments
    • new_blog_comment not new_comment
    • ...

    So the helper you're looking for is blog_comment_url. Also, since the resource is nested, the helper will want both the comment and blog as arguments:

    json.url blog_comment_url(comment.blog, comment, format: :json)