简体   繁体   中英

Rails admin: How add helpers and use view helpers?

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:

在此处输入图像描述

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM