# GET /tabs/1
# GET /tabs/1.xml
def show
@tab = Tab.find(params[:id])
@videos = Video.find(:all, :conditions => ["tab_id = ?", params[:id]])
@video = @tab.videos.build@comment = @tab.comments.build
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @tab }
end
end
I have objects called tab. Users can add comments and videos to tabs.
From the show view I want to be able to create a video and comment. It
goes fine for the videos. That's why the @tab.videos/comments.build are
there.
Now. As said everything is ok with the videos.
In the view I have one part with this:
<p id="comment_list">
<% for comment in @tab.comments.reverse %>
<b><%=h comment.created_by %></b> <%=h
comment.created_at.to_s(:short) %> <%=h comment.body %><br>
<% end %>
</p>
So I can simply list the comments for a tab.
But but, when I have the line @comment = @tab.comments.build in the
controller. I get the next error:
wrong number of arguments (1 for 0)
In the line:
<b><%=h comment.created_by %></b> <%=h comment.created_at.to_s(:short)
%> <%=h comment.body %><br>
The strange thing is that if I remove the .to_s(:short) in that line
there is no problem.
I can't find a reason why the .to_s(:short) and @comment =
@tab.comments.build cause that.
That comment has yet to be saved and thus has no created_at. Well, to be precise, comment.created_at is nil. Now, NilClass#to_s exists, but takes no arguments. The Time#to_s has be redefined by ActiveSupport to take a format symbol. Your error is from a call to nil.to_s(:short)
In your view you could either test for nil? or do something like:
<%= comment.new_record? ? '(unsaved)' : comment.created_at.to_s(:short) %>
a test of comment.created_at.nil? would be an equally valid condition, but this seems more intention-revealing.
At this point, @tab.comments now contains a new, uninitialised comment
with nil valued attributes (including the timestamps).
comment.created_at.to_s(:short)
One of the comments is the above mentioned uninitialised one so for this
one comment.created_at is nil which responds to #to_s but not with an
argument.
If the @comment instance variable is there for a form, then use:
Ok. Now your comments helped me spotting an error in my code. The point
is that in the same "show" view I can list the existing comments for
that tab and also create a new comment for the tab. Separate issues.
So I use one object to read the existing ones:
@comments = Comment.find(:all, :conditions => ["tab_id = ?",
params[:id]])
And another to be able to create new one:
@comment = @tab.comments.build