1

I'm trying to format some currency prices to show in list of Rails Admin.

I tried to define a helper in two ways app/helpers/rails_admin.rb:

module RailsAdmin
  include ActionView::Helpers::NumberHelper

  def price_mask_real(price)
    number_to_currency(price, unit: 'R$', separator: ',', delimiter: '.')
  end
end  

AND

module RailsAdmin::ViewsHelper
  include ApplicationHelper
end  

Having now the helper in application like:

module ApplicationHelper
  def price_mask_real(price)
    number_to_currency(price, unit: 'R$', separator: ',', delimiter: '.')
  end
end  

And in config/initializers/rails_admin.rb

list do
  configure :price do
    price_mask_real(:price)
  end
end

But Rails Admin can't find the helper:

enter image description here

João Ramires
  • 723
  • 10
  • 23
  • Does this answer your question? [Rails 3. How to add a helper that ActiveAdmin will use?](https://stackoverflow.com/questions/8673112/rails-3-how-to-add-a-helper-that-activeadmin-will-use) – Dan PZ Sep 01 '20 at 18:56
  • I tried what they said and the problem still `undefined method `price_mask_real' for #` – João Ramires Sep 01 '20 at 19:09

1 Answers1

1

You need to make rails_admin controllers inherit the class that has

include ApplicationHelper

Most of the time that is the ApplicationController

In that case adding this line to the rails admin initializer config file would do it

# config/initializers/rails_admin.rb
RailsAdmin.config do |config|
  config.parent_controller = '::ApplicationController'
end
Guillermo Siliceo Trueba
  • 4,251
  • 5
  • 34
  • 47