Ruby on rails created_at format show -


i added time method application_helper.rb.

i want show in view <%= message.created_at %> exist, how can use relative type instead of <%= message.created_at %> display such 2 minutes ago, 1 week ago

def relative_time(start_time)   diff_seconds = time.now - start_time    case diff_seconds   when 0 .. 59     puts "#{diff_seconds} seconds ago"   when 60 .. (3600-1)     puts "#{diff_seconds/60} minutes ago"   when 3600 .. (3600*24-1)     puts "#{diff_seconds/3600} hours ago"   when (3600*24) .. (3600*24*30)      puts "#{diff_seconds/(3600*24)} days ago"   else     puts start_time.strftime("%m/%d/%y")   end end 

there method called time_ago_in_words (thanks pavan mentioning alias, used distance_in_time_in_words_from_now). change way sentences displayed, need configure locales:

en:   datetime:     distance_in_words:       half_a_minute: half minute       less_than_x_seconds:         one: less 1 second         other: less %{count} seconds       x_seconds:         one: 1 second         other: '%{count} seconds'       less_than_x_minutes:         one: less minute         other: less %{count} minutes       x_minutes:         one: 1 minute         other: '%{count} minutes'       about_x_hours:         one: 1 hour         other: %{count} hours       x_days:         one: 1 day         other: '%{count} days'       about_x_months:         one: 1 month         other: %{count} months       x_months:         one: 1 month         other: '%{count} months'       about_x_years:         one: 1 year         other: %{count} years       over_x_years:         one: on 1 year         other: on %{count} years       almost_x_years:         one: 1 year         other: %{count} years 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -