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…
On 26 May 2008, at 11:20, Dag S. 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
when you render a partial as collection like this:
<%= render :partial => “guestbook/post”, :collection => @guestbooks %>
you can’t access guestbook this way:
values handed to a partial as collection are known within the partial
with the partials name
Frederick C. wrote:
On 26 May 2008, at 11:20, Dag S. 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”
#######################
index (presentation)
#######################
<%= render :partial => 'header'%>
<%= render :partial => 'topmenu'%>
<%= h @spec.full_name.or_else("Your N.") %>'s Presentation
Presentation URL: <%= link_to presentation_for(@user),
presentation_for(@user) %>
<%= render :partial => ‘content’%>
<%= render :partial => “friendship/friends”%>
<%= render :partial => “blog”%>
<%= @guestbook_title %>
<%= render :partial => “guestbook/post”, :collection => @guestbooks %>
#####################
_post
#####################
<%= render :partial => "notes/note", :collection => guestbook.notes
%>
<% if logged_in? %>
<%= link_to_remote "Add a comment",
:url => new_note_path(guestbook),
:method => :get %>
<% end %>
######################
_note
######################
<% if logged_in? and note.authorized?(User.find(session[:user])) %>
<%= link_to_remote "(delete)",
:url => note_url(note.guestbook, note),
:method => :delete,
:confirm => 'Are you sure?' %>
<% end %>
<%= link_to note.user.name, presentation_for(note.user) %>
commented
<%= time_ago_in_words note.created_at %> ago:
<%= sanitize note.body %>
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 M. 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:
2:
3:
4: <%= render :partial => “notes/note”, :collection => post.notes %>
5:
6:
On 27 May 2008, at 10:53, Dag S. wrote:
the id of nil, use object_id
Extracted source (around line #3):
1:
2:
3:
4: <%= render :partial => “notes/note”, :collection =>
post.notes %>
5:
6:
Sounds like the array you’ve passed to render has a nil in it. How are
you populating it?
Fred
@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)
On 27 May 2008, at 11:18, Thorsten M. wrote:
<%= render :partial => “guestbook/post”, :object => @guestbook %>
Or is render :partial => “guestbook/post”, :collection =>
@guestbook.posts
what is intended ?
Fred