# Strange error. Don't know what to ask

**URL:** <https://www.ruby-forum.com/t/strange-error-dont-know-what-to-ask/134338>\
**Category:** Rails\
**Created:** [April 9, 2008, 10:21pm UTC](https://www.ruby-forum.com/t/strange-error-dont-know-what-to-ask/134338 "2008-04-09T22:21:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [April 9, 2008, 10:21pm UTC](https://www.ruby-forum.com/t/strange-error-dont-know-what-to-ask/134338/1 "2008-04-09T22:21:50Z")

</div>

Hi guys,

I’m puzzled by an error I’m getting in my app.

I have some declarations in my show method:

# 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:

\<% for comment in @tab.comments.reverse %\> **\<%=h comment.created\_by %\>** \<%=h comment.created\_at.to\_s(:short) %\> \<%=h comment.body %\>  
 \<% end %\>

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:  
**\<%=h comment.created\_by %\>** \<%=h comment.created\_at.to\_s(:short)  
%\> \<%=h comment.body %\>

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.

Do you have any hints?

Thanks.

---

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [April 9, 2008, 10:37pm UTC](https://www.ruby-forum.com/t/strange-error-dont-know-what-to-ask/134338/2 "2008-04-09T22:37:12Z")

</div>

On Apr 9, 2008, at 4:21 PM, comopasta Gr wrote:

> @tab = Tab.find(params[:id])
> 
> @tab.comments.build cause that.
> 
> Do you have any hints?
> 
> Thanks.

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.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [April 9, 2008, 10:41pm UTC](https://www.ruby-forum.com/t/strange-error-dont-know-what-to-ask/134338/3 "2008-04-09T22:41:26Z")

</div>

comopasta Gr wrote:

> @comment = @tab.comments.build

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:

@comment = Comment.new

instead.

---

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [April 10, 2008, 9:31am UTC](https://www.ruby-forum.com/t/strange-error-dont-know-what-to-ask/134338/4 "2008-04-10T09:31:47Z")

</div>

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

* * *

My error was that in the view I was not using the @comments to iterate  
the existing comments.

I was using: @tab.comments.reverse directly used from the view.

So, instead of using that I am now using @comments.reverse and the  
errors are gone.

Thanks!

---

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [April 10, 2008, 1:41am UTC](https://www.ruby-forum.com/t/strange-error-dont-know-what-to-ask/134338/5 "2008-04-10T01:41:53Z")

</div>

It’s probably because whatever object is being sent “to\_s” isn’t the  
object you think it is.

It’s probably nil, which would mean to\_s would work, but to\_s(:short)  
won’t.

After you’ve built, created\_at will be nil, which will set up the case  
that I just described.

Set an initial value for it in the controller, and all will be good.

@comment = @tab.comments.build(:created\_at =\> Time.now)

or

@comment = @tab.comments.build; @comment.created\_at = Time.now

Julian.

Learn Ruby on Rails! Check out the FREE VIDS (for a limited time)  
VIDEO #3 out NOW!  
[http://sensei.zenunit.com/](http://sensei.zenunit.com/)
