Search code examples
ruby-on-railsrubyruby-on-rails-3nested-resources

How to use Link_to with nested resources


I am a totally newbie in Rails.

I have created a web application, I can access through /posts/123/comments/ or /posts/123/comments/new, but i don't know how to use link_to in the index view to show a concrete comment, when i try to link it, appears "no route" or "undefined symbol".

I have a nested have_many relation between posts and comments defined in the models and in the routes.rb and post_comments GET /posts/:post_id/sensors(.:format) comments#index appears when I execute rake routes.

How I can do it?


Solution

  • After trying all of the answers it didnt completely work, but i found a way to resolve it. In a first moment, I used

    post_comments_url(@post,comment)

    where comment is the item inside a @post.each.

    It generates a "strange" route, using . instead / like "post/34/comments.2", i fixed it using the singular form:

    post_comment_url(@post,comment)

    Thanks for helping!