Uhm .. how did you get that command line "ri" documentation to work? The command exists on my system but it looks like the actual docs are missing, eg:
$ ri Time.strftime
Nothing known about Time.strftime
If anyone's wondering, %e = Day of the month with no leading zeros (0-31). This is missing in "ri strftime" for both Time and Date ("ri Date.strftime" returned no info for me).
Does anyone know how to add a more generic string formatting function to the environment file (or elsewhere), which is available to models, controllers and views? (eg str_to_webname?)
Regarding your question, you could extend the String class to add your own method which does this. Place this in a file in your lib directory and require it in environment.rb
Nifty piece of advice and I have already implemented it in my project however it seems to cause my functional tests to fail. Is there anything extra/special that needs to be done in other for it to work in my tests. I keep getting a 'wrong number of parameters' error for to_s.
@schlick, make sure each of your fixtures has the time column specified in it. It sounds like the time is returning "nil" which will result in the error you are receiving. If you still can't get it to work I recommend you post on railsforum.com about this.
With rails 2.0.2 and after adding ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS[:rfc3339] = '%Y-%m-%dT%H:%M:00.000Z' to environment.rb I get a "rake aborted! uninitialized constant ActiveSupport" each time I run rake db:migrate. I am unable to find why... someone can help?
Great site, thanks for the hard work!
Just a typo probably, but for the record and to encourage proper English usage:
The screen picture above shows the right prepositions (on %B %d at %I %M %p) but the video shows them reversed (at %B %d on %I %M %p).
Thank you! This really what i'm looking for!
I appreciate your greak work! Thanks again!
Great tips.
Uhm .. how did you get that command line "ri" documentation to work? The command exists on my system but it looks like the actual docs are missing, eg:
$ ri Time.strftime
Nothing known about Time.strftime
Is there any way to put the format string things in the file other than environment.rb?
Thanks for the great screencasts.
I'm big fan :)
Hi, thanks for the hint with Time::DATE_FORMATS, I've been looking for quite a while for something like this :)
You can push this even further be adding a alias_method for to_s in Time class like this:
class Time
alias_method :[], :to_s
end
Now you can do something like Time.now[:custom_format] etc.
There is no []-instance method for the time-class, so this does not destroy some other behaviour in your existing app.
Greetings, Christoph
@Rebort, hmm, how did you install Ruby? Did you compile it from the source? If so, you needed to do "make install-doc" command after "make install".
@eskim, yep, you can move this into a file in the "lib" directory. Then just "require" the file in your environment.rb.
Very nice show.
Thanks !
Thanks Ryan, another great screencast!
If anyone's wondering, %e = Day of the month with no leading zeros (0-31). This is missing in "ri strftime" for both Time and Date ("ri Date.strftime" returned no info for me).
Does anyone know how to add a more generic string formatting function to the environment file (or elsewhere), which is available to models, controllers and views? (eg str_to_webname?)
@Zubin, cool, didn't know about %e.
Regarding your question, you could extend the String class to add your own method which does this. Place this in a file in your lib directory and require it in environment.rb
--
class String
def to_webname
#...
end
end
--
sorry there's no indentations
absolutely useful tip... even now that I know it, I still can't find the documentation for Time class... so imagine if I didn't know...
Nifty piece of advice and I have already implemented it in my project however it seems to cause my functional tests to fail. Is there anything extra/special that needs to be done in other for it to work in my tests. I keep getting a 'wrong number of parameters' error for to_s.
@schlick, make sure each of your fixtures has the time column specified in it. It sounds like the time is returning "nil" which will result in the error you are receiving. If you still can't get it to work I recommend you post on railsforum.com about this.
Thanks Ryan for the quick reply. And yes, you were right. I needed to add in values for my datetimes into my fixtures.
very cool~
Add This:
ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.update(:my_format => '%y-%m-%d %H:%M')
This appears to be broken in Rails 2.0 :*(
For Rails 2.0 you need to do like this in your config:
ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMATS[:due_on] = '%m-%d-%y'
:)
With rails 2.0.2 and after adding ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS[:rfc3339] = '%Y-%m-%dT%H:%M:00.000Z' to environment.rb I get a "rake aborted! uninitialized constant ActiveSupport" each time I run rake db:migrate. I am unable to find why... someone can help?
I get this error too. Anyone know how to avoid this?
GregH, akatako,
Try adding those after the
Rails::Initializer.run do |config|
..
end
ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.merge!(
...
)
Hope that helps.
I had a similar issue with ActionMailer as well.
For Rails 2.2:
# config/initializers/time_formats.rb
Time::DATE_FORMATS[:my_format] = "%B %Y"
Great site, thanks for the hard work!
Just a typo probably, but for the record and to encourage proper English usage:
The screen picture above shows the right prepositions (on %B %d at %I %M %p) but the video shows them reversed (at %B %d on %I %M %p).
This episode has been updated for Rails 5 as a blog post. Customizing Time Format in Rails 5 Apps
Thank you so much, great advice !