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

1 Answer 1

8

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: руб.
13
  • 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, 2014 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, 2014 at 11:22
  • Mister are you wizard. Thanks a lot ;)
    – dPanda13
    May 21, 2014 at 11:25
  • hmmm maybe its weird but nothing is going on i still see USD
    – dPanda13
    May 21, 2014 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, 2014 at 11:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.