3

I building a administration interface via ActiveAdmin. I have some resources like Products, at the Products i have an Article, Title, Description and Price columns. Everything working well, but i have a little problem, at the Price column i use helper number_to_currency, by default ActiveAdmin display currency as USD. I want to display prices in local currency, so and here i have a question how to implement this helper to display price in local currency (for example FR, AUD or RUB).


Rails 4.1.0

ActiveAdmin 1.0.0

ruby 2.1


app/admin/product.rb

ActiveAdmin.register Product do

    # Permitted parameters
    permit_params :article_id, :title, :description, :price

    # Displayed columns
    index do
        column :article, :sortable => :article
        column :title
        column :description
    # Currency helper
        column :price, :sortable => :price do |cur|
            number_to_currency cur.price
        end
        default_actions
    end
end

app/models/product.rb

class Product < ActiveRecord::Base

    # Relationship
    belongs_to :article

    # Validations
    validates :article, :title, :description, :price, :presence => true
end
dPanda13
  • 265
  • 1
  • 3
  • 9

1 Answers1

7

Use the :locale parameter. From the documentation:

Options


:locale - Sets the locale to be used for formatting (defaults to current locale)

...snip...

number_to_currency(1234567890.506, locale: :fr)      # => 1 234 567 890,51 €

To add support for a locale you need to have a <locale>.yml under config/locale, for example:

ru.yml

ru:
  number:
    currency:
      format:
        delimiter: ! ','
        format: ! '%n %u'
        precision: 2
        separator: '.'
        unit: руб.
Community
  • 1
  • 1
Uri Agassi
  • 36,848
  • 14
  • 76
  • 93
  • sorry, i need to implement this inside a app/admin/product.rb or ill need to make a helper file and put require inside app/admin/product.rb ? Also thanks a lot for your help again ;) – dPanda13 May 21 '14 at 11:18
  • This is built into the `number_to_currency` API (see the link) - all you need to do is add locale information to its call (`number_to_currency cur.price, locale: my_locale`) – Uri Agassi May 21 '14 at 11:22
  • Mister are you wizard. Thanks a lot ;) – dPanda13 May 21 '14 at 11:25
  • hmmm maybe its weird but nothing is going on i still see USD – dPanda13 May 21 '14 at 11:39
  • What locale are you setting? you can test it in `rails c` before running it on your server – Uri Agassi May 21 '14 at 11:41
  • already tried **number_to_currency cur.price, locale: :fr** and **number_to_currency cur.price, locale: :ru** and still see a USD – dPanda13 May 21 '14 at 11:47
  • also i tried do define ru.yml in config/locales/ **ru: number: currency: format: unit: 'руб.' delimiter: ',' separator: '.' precision: 2 format: '%u%n'** and i still see prices in USD :) – dPanda13 May 21 '14 at 12:22
  • this helps: http://stackoverflow.com/questions/7237592/number-to-currency-locale-converting ? – Uri Agassi May 21 '14 at 12:26
  • Add the following files - it worked for me: ru.yml: https://raw.githubusercontent.com/svenfuchs/rails-i18n/master/rails/locale/ru.yml; fr.yml: https://raw.githubusercontent.com/svenfuchs/rails-i18n/master/rails/locale/fr.yml – Uri Agassi May 21 '14 at 12:30
  • its kind of buggy stuff because i copied this to **ru.yml** in **/config/locales** after that in **app/admin/product.rb** i call **I18n.locale = :ru** locales applies for everything but not for this Price column, its so weird – dPanda13 May 21 '14 at 12:34
  • I've created the following ru.yml, which works for me: https://gist.github.com/uriagassi/5f448b7712817653b507 – Uri Agassi May 21 '14 at 12:41
  • wicked! it works! Also i've **call number_to_currency cur.price, unit: 'руб.'**, and its works too, but have another problem )) currency sign now displays before value, i need that it displays after value – dPanda13 May 21 '14 at 12:47
  • wicked! now everything is working properly. Uri, Thanks a lot for your time. – dPanda13 May 21 '14 at 12:58