i have dates and times stored in a database and need to reformat for display... what is the ''rails way'' to do the formatting... in a helper? in the model itself? _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Sean T Allen <sean-5W9FBhQXBOtBDgjK7y7TUQ@public.gmane.org> writes:> i have dates and times stored in a database and need to reformat for > display...app/views/model_name/action.rthml: <%= @obj.time_attribute.strftime("%Y-%m-%d %H:%M:%S") %> -- doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.org
If you want to collect common view formatting, just create a method in your application helper file. You can then use it from any view and update the format in one place. Robert On Apr 29, 2005, at 11:07 AM, Sean T Allen wrote:> Doug Alcorn wrote: > >> Sean T Allen <sean-5W9FBhQXBOtBDgjK7y7TUQ@public.gmane.org> writes: >> >> >>> i have dates and times stored in a database and need to reformat for >>> display... >>> >> >> app/views/model_name/action.rthml: >> >> <%= @obj.time_attribute.strftime("%Y-%m-%d %H:%M:%S") %> >> >> > I would end up having this all over the place though... the concept > of having to change that all over the place in templates seems > completely wrong to > me. If i want to change the format, I should have it centralized in > one place... > which comes back to... where would i centralize... would putting it in > the model > cause problems? a helper? a shared partial? > > <sean.vcf>_______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
On 29.4.2005, at 21:07, Sean T Allen wrote:> Doug Alcorn wrote: > >> Sean T Allen <sean-5W9FBhQXBOtBDgjK7y7TUQ@public.gmane.org> writes: >> >> >>> i have dates and times stored in a database and need to reformat for >>> display... >>> >> >> app/views/model_name/action.rthml: >> >> <%= @obj.time_attribute.strftime("%Y-%m-%d %H:%M:%S") %> >> >> > I would end up having this all over the place though... the concept > of having to change that all over the place in templates seems > completely wrong to > me. If i want to change the format, I should have it centralized in > one place... > which comes back to... where would i centralize... would putting it in > the model > cause problems? a helper? a shared partial?Put a new file in lib where you extend Time class with your own formatting function. Then you can call that function for any Time object in your app: Class Time def short_format self.strftime("%Y-%m-%d") end end //jarkko> > <sean.vcf>_______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Doug Alcorn wrote:>Sean T Allen <sean-5W9FBhQXBOtBDgjK7y7TUQ@public.gmane.org> writes: > > > >>i have dates and times stored in a database and need to reformat for >>display... >> >> > >app/views/model_name/action.rthml: > ><%= @obj.time_attribute.strftime("%Y-%m-%d %H:%M:%S") %> > > >I would end up having this all over the place though... the concept of having to change that all over the place in templates seems completely wrong to me. If i want to change the format, I should have it centralized in one place... which comes back to... where would i centralize... would putting it in the model cause problems? a helper? a shared partial? _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Ah perfect... thanks... Robert Bousquet wrote:> If you want to collect common view formatting, just create a method in > your application helper file. You can then use it from any view and > update the format in one place. > > Robert > > > On Apr 29, 2005, at 11:07 AM, Sean T Allen wrote: > >> Doug Alcorn wrote: >> >>> Sean T Allen <sean-5W9FBhQXBOtBDgjK7y7TUQ@public.gmane.org> writes: >>> >>> >>>> i have dates and times stored in a database and need to reformat >>>> for display... >>>> >>> >>> app/views/model_name/action.rthml: >>> >>> <%= @obj.time_attribute.strftime("%Y-%m-%d %H:%M:%S") %> >>> >>> >> I would end up having this all over the place though... the concept >> of having to change that all over the place in templates seems >> completely wrong to >> me. If i want to change the format, I should have it centralized in >> one place... >> which comes back to... where would i centralize... would putting it >> in the model >> cause problems? a helper? a shared partial? >> >> <sean.vcf>_______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Sean T Allen wrote:> Doug Alcorn wrote: > >> Sean T Allen <sean-5W9FBhQXBOtBDgjK7y7TUQ@public.gmane.org> writes: >> >> >> >>> i have dates and times stored in a database and need to reformat for >>> display... >>> >> >> app/views/model_name/action.rthml: >> >> <%= @obj.time_attribute.strftime("%Y-%m-%d %H:%M:%S") %> >> >> >> > I would end up having this all over the place though... the concept > of having to change that all over the place in templates seems > completely wrong to > me. If i want to change the format, I should have it centralized in one > place... > which comes back to... where would i centralize... would putting it in > the model > cause problems? a helper? a shared partial?You could put a virtual accessor in your model... def formatted_time self.time_atribute.strftime("%Y-%m-%d %H:%M:%S") end - -- David Morton Maia Mailguard server side anti-spam/anti-virus solution: http://www.maiamailguard.com -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.5 (GNU/Linux) Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org iD8DBQFCcnpoSIxC85HZHLMRAhczAJ90+7DjA2FHoy+EoqXe9ydalqqtowCfcskQ VqpVjVEyQsT4ijsC4lyCojA=ig1x -----END PGP SIGNATURE-----
Sean T Allen <sean-5W9FBhQXBOtBDgjK7y7TUQ@public.gmane.org> writes:>>app/views/model_name/action.rthml: >> >><%= @obj.time_attribute.strftime("%Y-%m-%d %H:%M:%S") %> >> >> >> > I would end up having this all over the place though... the concept > of having to change that all over the place in templates seems > completely wrong to me. If i want to change the format, I should > have it centralized in one place...Ok, I''m just guessing now. lib/standard_date.rb class Date def standard_date self.strftime("%Y %m %d %H:%M:%S") end end This would allow you to simply do obj.time_attribute.standard_date. You''d have to require ''standard_date'' somewhere. I''m not sure where to do that, I guess you could do it in environment.rb. -- doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.org
> > Put a new file in lib where you extend Time class with your own > formatting function. Then you can call that function for any Time > object in your app: > > Class Time > def short_format > self.strftime("%Y-%m-%d") > end > end >Ok... i have a question related to this... if i call said file say... formats.rb it doesnt get loaded... if i call it time.rb it does however if i create a date.rb ( for extending the Date class )then i cant start rails as it complains about the method today not existing... can someone explain this to me? my current solution just puts it both in time.rb well while i await a response i shall try and figure out where in the pick axe book look... ah the joys of learning a new language and format at the same time. _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Doug Alcorn wrote:>Sean T Allen <sean-5W9FBhQXBOtBDgjK7y7TUQ@public.gmane.org> writes: > > > >>>app/views/model_name/action.rthml: >>> >>><%= @obj.time_attribute.strftime("%Y-%m-%d %H:%M:%S") %> >>> >>> >>> >>> >>> >>I would end up having this all over the place though... the concept >>of having to change that all over the place in templates seems >>completely wrong to me. If i want to change the format, I should >>have it centralized in one place... >> >> > >Ok, I''m just guessing now. > >lib/standard_date.rb > >class Date > def standard_date > self.strftime("%Y %m %d %H:%M:%S") > end >end > >This would allow you to simply do obj.time_attribute.standard_date. >You''d have to require ''standard_date'' somewhere. I''m not sure where >to do that, I guess you could do it in environment.rb. > >ah and that answers my other question... environment.rb... i''m learning so much today... thanks to everyone... _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Sean T Allen <sean-5W9FBhQXBOtBDgjK7y7TUQ@public.gmane.org> writes:> if i call it time.rb it does however if i create a date.rb ( for > extending the Date class )then i cant start rails as it complains > about the method today not existing...If you call it time.rb it''s probably loading your file in lieu of the standard time.rb and not getting all the other good stuff. -- doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.org
Is there any way to do this transparently/automatic if the column starts with some predefined string , for example: All fields starting with: data_ are shown in the view with an certing format.. and are saved in the database with the standard format ? I want to use an text field for dates in my form, and i dont want to call a function to format the date in every view, and reformat again in every create,update action thx 2005/4/29, Doug Alcorn <doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.org>:> > Sean T Allen <sean-5W9FBhQXBOtBDgjK7y7TUQ@public.gmane.org> writes: > > > if i call it time.rb it does however if i create a date.rb ( for > > extending the Date class )then i cant start rails as it complains > > about the method today not existing... > > If you call it time.rb it''s probably loading your file in lieu of the > standard time.rb and not getting all the other good stuff. > -- > doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.org > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Giovanni Degani tiefox-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org ICQ 965609 _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On 7/22/05, Giovanni Degani <tiefox-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Is there any way to do this transparently/automatic if the column starts > with some predefined string , for example: > All fields starting with: data_ > are shown in the view with an certing format.. > and are saved in the database with the standard format ?One thing you can do now is defined your own date formats. There''s #to_formatted_s date method with some default ones, such as :db. Add your own like this (in your environment.rb): ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.merge!( :my_format_1 => ''%l %p, %b %d, %Y'', :my_format_2 => ''%l:%M %p, %B %d, %Y'' ) <%= @post.created_at.to_formatted_s(:my_format_1) %> -- rick http://techno-weenie.net