Hi everyone. I just ran into an issue which I solved in an...er...interesting way, and I''m trying to figure out if my solution is brilliant or terrible. Basically, I have a Date attribute in one of my AR models which I would like my views to display with a particular format (other than what Date#to_s gives). Rather than have to call a formatting method on this attribute every time I use it in a view, what I have done is to override the to_s method on the object''s singleton class. Does this make sense? Is there a better way to handle date formatting in Rails? Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Thu, Jan 21, 2010 at 7:34 PM, Marnen Laibow-Koser <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hi everyone. > > I just ran into an issue which I solved in an...er...interesting way, > and I''m trying to figure out if my solution is brilliant or terrible. > > Basically, I have a Date attribute in one of my AR models which I would > like my views to display with a particular format (other than what > Date#to_s gives). Rather than have to call a formatting method on this > attribute every time I use it in a view, what I have done is to override > the to_s method on the object''s singleton class. > > Does this make sense? Is there a better way to handle date formatting > in Rails? >There is a way to specify different date formats in rails, checkout the "Adding your own time formats to to_formatted_s" section of the API. http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Date/Conversions.html -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Christos Trochalakis wrote:> On Thu, Jan 21, 2010 at 7:34 PM, Marnen Laibow-Koser > <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> >> Does this make sense? Is there a better way to handle date formatting >> in Rails? >> > > There is a way to specify different date formats in rails, checkout > the "Adding your own time formats to to_formatted_s" section of the > API. > > http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Date/Conversions.htmlThanks! I *thought* Rails had something like this, but I was looking in the wrong place. Duhhh. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. --00151747b52aa4c23d047db16f5b Content-Type: text/plain; charset=ISO-8859-1 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. --00151747b52aa4c23d047db16f5b--