D
D
dexdev2014-09-04 16:29:10
Ruby on Rails
dexdev, 2014-09-04 16:29:10

How to display user age in rails?

I filed the age of the users, now I can’t figure out how to add the correct declination of “years” “years” “year” to them at the end
User.rb

def age
    now = Time.now.utc.to_date
    now.year - birthday.year - (birthday.to_date.change(:year => now.year) > now ? 1 : 0)
  end

I insert into the view
<%= @user.age %>
and what to add so that not only the number is displayed, but it was, for example, 25 years 32 years old?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
anyd3v, 2014-09-04
@AdilA

Rails has a helper for this, but it doesn't work properly with Russian. Look aside https://github.com/yaroslav/russian/
there is an example
Russian::pluralize
Russian::p
Russian.p(1, "thing", "things", "things")
=> "thing"
Russian .p(2, "thing", "things", "things")
=> "things"
Russian.p(10, "thing", "things", "things")
=> "things"
Russian.p(3.14 , "thing", "things", "things", "things") # the last option is used for fractional values
​​=> "things"
in your case will be something like <%= @user.age Russian.p(@user. age, "..

A
Alexey, 2014-09-06
@fuCtor

It is better to use the locale and the corresponding API:
rusrails.ru/rails-internationalization-i18n-api
Your case section 4.3

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question