Forum: Ruby on Rails question about strftime when called from partial.

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
036be8d5d83a98e546b98d64e3fd6c20?d=identicon&s=25 Sam Donaldson (Guest)
on 2006-05-16 21:22
(Received via mailing list)
Hi,

I'm calling a helper I've written - format_time() that is called from my
views with a given time that was selected from my database and was
returned
to the view via an instance variable.  When I call format_time from a
partial, I see that I get some kind of String error and the partial
won't
render.  Basically, the object being passed in to format_time is already
a
string and not in the Time format?   Anything special going on with
partials?

Thanks,.
3dd4b52a0946bd698b1d1635a46ea3a3?d=identicon&s=25 François Beausoleil (fbeausoleil)
on 2006-05-17 01:48
(Received via mailing list)
Hello Sam,

2006/5/16, Sam Donaldson <samonderous@gmail.com>:
> I'm calling a helper I've written - format_time() that is called from my
> views with a given time that was selected from my database and was returned
> to the view via an instance variable.  When I call format_time from a
> partial, I see that I get some kind of String error and the partial won't
> render.  Basically, the object being passed in to format_time is already a
> string and not in the Time format?   Anything special going on with
> partials?

Can you be more specific ?  Show us some code ?  I do that all the
time, and I have zero problems.

Bye !
036be8d5d83a98e546b98d64e3fd6c20?d=identicon&s=25 Sam Donaldson (Guest)
on 2006-05-17 06:04
(Received via mailing list)
So here is the code:

module ApplicationHelper

  def format_time(time)
    time.strftime("%a %b %d, %I:%M%p")
  end
end

and the partial gets the following error;

undefined method `strftime' for "2006-05-13 15:15:00":String

So basically it is looking at the object as a String instead of a Time
object which is odd.

Thanks.
036be8d5d83a98e546b98d64e3fd6c20?d=identicon&s=25 Sam Donaldson (Guest)
on 2006-05-17 06:20
(Received via mailing list)
I want to add one more thing in case somebody can figure this out, so I
created another helper just for that partial called:

def format_date(time)
    new_time = Time.local(*(ParseDate.parsedate(time)))
    new_time.strftime("%a %b %d, %I:%M%p")
 end

And this worked.  So then the question is, why is it that it is coming
back
as a String rather than a Time object.  The above code just takes that
string and converts it to a Time object and then does the same
formatting ?

Thanks.
58479f76374a3ba3c69b9804163f39f4?d=identicon&s=25 Eric Hodel (Guest)
on 2006-05-17 20:04
(Received via mailing list)
On May 16, 2006, at 9:03 PM, Sam Donaldson wrote:

> undefined method `strftime' for "2006-05-13 15:15:00":String
> So basically it is looking at the object as a String instead of a
> Time object which is odd.

Replace that with:

class Time
   def format
     strtime "%a %b %d, %I:%M%p"
   end
end

Then just call format on your Time instance directly.

--
Eric Hodel - drbrain@segment7.net - http://blog.segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com
This topic is locked and can not be replied to.