doesn't create my guestbook

Hi,

I'm having some trouble with my realtions (I think). I'm creating a guestbook with comments in ajax (the structure resembles the structure of a blog). So I've got a "flogs table" which keeps the "user_id" and the "id" of the flogs. This table. Then I've got a "guestbooks table" containing "id" of the guestbooks, "flog_id", "title", "body", "created_at" and "updated_at". This one does NOT get created, resulting in (for example) "nil.title". (guestbook.title)

Then I've got a "notes table" containing "id" of the notes, "user_id", "guestbook_id", "body" and "created_at".

The realtions may be wrong here (at least that is what I suspect although I can't see why they wouldn't).

user.rb has_many :notes,          :order => "created_at DESC",          :dependent => :destroy has_one :flog

flog.rb belongs_to :user has_many :guestbooks, :order => "created_at DESC"

guestbook.rb belongs_to :flog has_many :notes, :order => "created_at", :dependent => :destroy

note.rb belongs_to :user belongs_to :guestbook

furthermore, even if I manually add content to the table "guestbooks" i still get the same error...

You've shown us the relations (which appear at least vaguely sane (although I'm not entirely sure of the point of the flogs tables)) but not any of the code that is producing the error.

Fred

Frederick Cheung wrote:

in (for example) "nil.title". (guestbook.title)

Then I've got a "notes table" containing "id" of the notes, "user_id", "guestbook_id", "body" and "created_at".

The realtions may be wrong here (at least that is what I suspect although I can't see why they wouldn't).

You've shown us the relations (which appear at least vaguely sane (although I'm not entirely sure of the point of the flogs tables)) but not any of the code that is producing the error.

Fred

Okay, well, thanks Fred for checking it.

Scratched the flog-table and placed the user_id in the guestbook table instead. If my relations are right, the fault may be in my Ajax code (and I'm very newbee to Ajax) so maybe this is what's wrong.

The main problem being "undefined local variable or method `guestbook' for #<#<Class:0xaada638>:0xaada610>" when it gets the partial _post: "app/views/guestbook/_post.rhtml"

when you render a partial as collection like this:

<%= render :partial => "guestbook/post", :collection => @guestbooks %>

you can't access guestbook this way:

  <div id="notes_for_guestbook_<%= guestbook.id %>">

values handed to a partial as collection are known within the partial with the partials name

<div id="notes_for_guestbook_<%= post.id %>">

should work.

Thorsten Mueller wrote:

when you render a partial as collection like this:

<%= render :partial => "guestbook/post", :collection => @guestbooks %>

you can't access guestbook this way:

  <div id="notes_for_guestbook_<%= guestbook.id %>">

values handed to a partial as collection are known within the partial with the partials name

<div id="notes_for_guestbook_<%= post.id %>">

should work.

Okay, thanks for explaining that, made a few more changes inside my partial after that, but now it renders a completely different error instead:

"Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id"

I'm getting somewhat confused, is should get the id now (which exists in my guestbook table). Why does it get a nil? and what does 4 mean?

I'm getting somewhat confused, is should get the id now (which exists in my guestbook table). Why does it get a nil? and what does 4 mean?

get this error at the same place in code? (should not, since the partial is rendered as a collection...)

the 4 is easy: it's just the default id of the nil object. type nil.id in irb and you get this as result (together with the deprecated warning)

pls post the error message, so we can see in which line it happens.

Thorsten Mueller wrote:

get this error at the same place in code? (should not, since the partial is rendered as a collection...)

Same place in code

the 4 is easy: it's just the default id of the nil object. type nil.id in irb and you get this as result (together with the deprecated warning)

okay, finally an explaination ;D

pls post the error message, so we can see in which line it happens.

RuntimeError in Presentation#show

Showing app/views/guestbook/_post.rhtml where line #3 raised:

Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id

Extracted source (around line #3):

1: <div class="guestbook"> 2: 3: <div id="notes_for_guestbook_<%= post.id %>"> 4: <%= render :partial => "notes/note", :collection => post.notes %> 5: </div> 6:

Sounds like the array you've passed to render has a nil in it. How are you populating it?

Fred

Frederick Cheung wrote:

@guestbook = @user.guestbook ||= Guestbook.new

in this case you get a single ActiveRecord object instead of the list of search results you would get with for example @guestbook = Guestbook.find(:all,...)

to make this run you must use

<%= render :partial => "guestbook/post", :object => @guestbook %>

(and you mix the plural/singular form of @guestbook, make sure to use this consistent)

@guestbook = @user.guestbook ||= Guestbook.new

in this case you get a single ActiveRecord object instead of the
list of search results you would get with for example @guestbook = Guestbook.find(:all,...)

to make this run you must use

<%= render :partial => "guestbook/post", :object => @guestbook %>

Or is render :partial => "guestbook/post", :collection =>
@guestbook.posts

what is intended ?

Fred