# Problem with Getting Started with Rails

**URL:** <https://www.ruby-forum.com/t/problem-with-getting-started-with-rails/185185>\
**Category:** Rails\
**Created:** [March 15, 2010, 6:37pm UTC](https://www.ruby-forum.com/t/problem-with-getting-started-with-rails/185185 "2010-03-15T18:37:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![neilbain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/b487fb/32.png) [@neilbain](https://www.ruby-forum.com/u/neilbain)\
**Post date:** [March 15, 2010, 6:37pm UTC](https://www.ruby-forum.com/t/problem-with-getting-started-with-rails/185185/1 "2010-03-15T18:37:45Z")

</div>

I am working through the official Getting Started with Rails tutorial

> **[Getting Started with Rails — Ruby on Rails Guides](https://guides.rubyonrails.org/getting_started.html#generating%20-a-controller)**
>
> Getting Started with RailsThis guide covers getting up and running with Ruby on Rails.After reading this guide, you will know: How to install Rails, create a new Rails application, and connect your application to a database. The general layout of a...

I am stuck on section 8.4 Generating a Controller. It suggests there  
that this definition:  
def index  
@post = Post.find(params[:post\_id])  
@comments = @post.comments  
end

S ould be used in com ents\_controller.rb. Surely that would make  
[http://localhost:3000/comments](http://localhost:3000/comments) work, but it gives the error  
Couldn’t find Post without an ID. That suggests a problem with the first  
line of the definition. However that is what it says I should use in the  
tutorial so I looked at post.rb and comment.rb and they are as directed.  
Also checked routes.rb but all seems to be correct. Incidentally the  
page [http://localhost:3000/posts/1](http://localhost:3000/posts/1)  
works fine and displays comments which I have to enter with the terminal  
as [http://localhost:3000/comments/new](http://localhost:3000/comments/new) gives the same error.

I have been tearing my hair out with this for 3 days. Can somebody  
please point out what I am doing wrong.

---

<div class="post-metadata">

**Author:** ![neilbain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/b487fb/32.png) [@neilbain](https://www.ruby-forum.com/u/neilbain)\
**Post date:** [March 15, 2010, 9:43pm UTC](https://www.ruby-forum.com/t/problem-with-getting-started-with-rails/185185/2 "2010-03-15T21:43:09Z")

</div>

Neil B. wrote:

> I am working through the official Getting Started with Rails tutorial  
> [Getting Started with Rails — Ruby on Rails Guides](http://guides.rubyonrails.org/getting_started.html#generating%20-a-controller)
> 
> I am stuck on section 8.4 Generating a Controller. It suggests there  
> that this definition:  
> def index  
> @post = Post.find(params[:post\_id])  
> @comments = @post.comments  
> end
> 
> S ould be used in com ents\_controller.rb. Surely that would make  
> [http://localhost:3000/comments](http://localhost:3000/comments) work, but it gives the error  
> Couldn’t find Post without an ID. That suggests a problem with the first  
> line of the definition. However that is what it says I should use in the  
> tutorial so I looked at post.rb and comment.rb and they are as directed.  
> Also checked routes.rb but all seems to be correct. Incidentally the  
> page [http://localhost:3000/posts/1](http://localhost:3000/posts/1)  
> works fine and displays comments which I have to enter with the terminal  
> as [http://localhost:3000/comments/new](http://localhost:3000/comments/new) gives the same error.

Take a closer look at this URL:  
[http://localhost:3000/comments](http://localhost:3000/comments)

How would the comments\_controller know which post to look up. The  
resource identifier is not anywhere in the URL and it’s not tacked on as  
a query parameter.

The most likely design in this case would be to nest comments under  
posts. The URL would look something like:

[http://localhost:3000/posts/1/comments/new](http://localhost:3000/posts/1/comments/new)

And the new action would look something like:

def new  
@post = Post.find(params[:post\_id])  
@comment = @post.comments.build  
end

If course you’re going to want to DRY up this code by moving:

@post = Post.find(params[:post\_id])

Into a private method and adding a before\_filter to load the post.
