1

I have a view of my model with many date fields rendered.

What I want to do is call strftime('%d.%m.%Y %H:%M') on each date field I have.

- @items.each do |item| 
  = item.date_field.strftime('%d.%m.%Y %H:%M') 
  = item.date_field1.strftime('%d.%m.%Y %H:%M')

Any ideas on how I can to DRY this? I tried to make to_s method in my model, but unfortunately that doesn't work.

Update:

I defined this method:

def to_s(date)
  send(date).strftime('%d.%m.%Y %H:%M')
end

So I can:

- @items.each do |item| 
  = item.to_s(:date_field)
  = item.to_s(:date_field1)
SumLare
  • 45
  • 1
  • 5
  • 1
    Possible duplicate of [Changing the default date and time format in Rails 4](http://stackoverflow.com/questions/17870762/changing-the-default-date-and-time-format-in-rails-4) – Eduardo Sampaio Oct 25 '16 at 13:45
  • Can you post the view or an example of it? – C dot StrifeVII Oct 25 '16 at 13:45
  • @CdotStrifeVII Example: `- @items.each do |item|` `= item.date_field.strftime('%d.%m.%Y %H:%M')` `= item.date_field1.strftime('%d.%m.%Y %H:%M')` `= ...` – SumLare Oct 25 '16 at 13:50
  • Not supporting the close vote on this, the question, as phrased, is not about **changing the default time format** but **drying up code in views**. – Jonathan Allard Oct 25 '16 at 14:05
  • Define "doesn't work". Please add code to the question, not to comments--it's impossible to understand your intent. – Dave Newton Oct 25 '16 at 14:07
  • I just came up with this bunch of code in my model `def to_s(date)` `send(date).strftime('%d.%m.%Y %H:%M')` `end` – SumLare Oct 25 '16 at 14:10
  • I may have been misunderstood. **Please add code to the question, not to comments.** I don't understand why you're using `send` there, you can call `strftime` directly on the `date` parameter. If nothing else, you're using `send` wrong. – Dave Newton Oct 25 '16 at 14:12
  • I will really prefer either `helper` or `localization` with customized format – Deepak Mahakale Oct 25 '16 at 14:15
  • @SumLare What happens if you call `item.id.to_s` or any other field? – Deepak Mahakale Oct 25 '16 at 14:33
  • @Deepak I got it now, I'll use option 2 for it – SumLare Oct 25 '16 at 14:47

1 Answers1

4

You can create a helper (in application_helper.rb if you are using it application wide)

Option 1

def customized_time_format(time)
  time.strftime('%d.%m.%Y %H:%M')
end

and make use of it in your views

- @items.each do |item| 
  = customized_time_format(item.date_field)
  = customized_time_format(item.date_field1)

Option2

# config/locales/en.yml
en:
  time:
    formats:
      customized: '%d.%m.%Y %H:%M'

And use it

- @items.each do |item| 
  = l(item.date_field, format: :customized)
  = l(item.date_field1, format: :customized)
Deepak Mahakale
  • 22,834
  • 10
  • 68
  • 88
  • That is good approach, but I need this method only for one model. I guess `customized_time_format` method is better to be defined in my model. – SumLare Oct 25 '16 at 14:14
  • @SumLare I would use option 2 personally, but if you use 1 I wouldn't put it in your model, if you need it on another model later you have to move it. – j-dexx Oct 25 '16 at 14:31
  • Option 2 is much better IMO – Juan Fuentes Oct 25 '16 at 14:35