15 minute RoR blog help


I’m a newbie to rails, ruby, and web development in general, and I’m
having trouble with belongs_to and has_xxx relationships.

For example, right now I’m trying to get the 15 minute blog which is
listed on the page to work, and when I try to post a comment, I’m
getting this message:

“Couldn’t find Post without an ID”

Also, when I try and scaffold the comments attribute, post_id does not
show up on the list. However, it seems to be there when I look at the
schema file:

create_table “comments”, :force => true do |t|
t.column “body”, :text
t.column “post_id”, :integer

I’m thinking the post_id attribute not showing up as a parameter when I
run the scaffold and the “Couldn’t find Post without an ID” is related.

This is the code in the controller:

def comment
flash[:notice] = “Added your comment”
redirect_to :action => “show”, :id => params[:id]

And this is the code that I’m trying to run:

<%= form_tag :action => “comment”, :id => @post %>
<%= text_area “Comment”, “body” %>

<%= submit_tag “Comment!” %>

Very concrete help is really appreciated, since I’m quite new at both
rails and ruby. Also, does anyone know where I can find the source code
to the 15 minute blog? I keep checking and double checking my
code/databases/etc. against what’s in the video but I’m not seeing the
problem. Thank you very much!

Mariko, note that your error message says:

“Couldn’t find Post without an ID”

Which means that ActiveRecord has been asked to go find a Post, but no
id was supplied. When you create a default model in a migration with
Rails like:

create_table :posts do |t|
t.column :some_column, :string

you will also get - automatically - a column called “id”. Just plain
“id”. Of course you also get your Post model and a bunch of other
stuff for free. In you Comment model (comments table) the column
called “post_id” is how Rails goes and finds the relevant Post object
in their belongs_to relationship.

So a few things to look at:
(a) Suggest you use the form_for version of forms tags when using a
model, rather than form_tag, it does more for you.
(b) Your view is refering to @post, but i see nothing that sets than
instance variable in your controller
© So I suspect the error you’re getting, assuning its when you
“submit” is that your find() is failing because no “id” is set. But
again, not the error is referring to the “id” column on the Post
object - not the “post_id” column on the Comment object.

A bit hard to tell from the elements of code you submitted. Happy to
take a quick look if you email the model, controller and view to me at
kipcole9 -at- gmail dot you-know-what.

Cheers, --Kip

On Aug 8, 12:12 pm, “Mariko C.” [email protected]