I am working through the official Getting Started with Rails tutorial
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 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
works fine and displays comments which I have to enter with the terminal
as 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.
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 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
works fine and displays comments which I have to enter with the terminal
as http://localhost:3000/comments/new gives the same error.
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: