Alexander Kireyev
Alexander Kireyev

Reputation: 10825

Unable to use ActiveAdmin helpers

I want to use custom methods in ActiveAdmin. I added helper file in app/helpers/active_admin/views_helper.rb:

module ActiveAdmin::ViewsHelper
  def currency_row(name)
    row name do
      number_to_currency(deal.send(name), precision: 0)
    end
  end
end

But I have next error:

undefined method `row' for #<#<Class:0x007fe83f0c0650>:0x007fe83f0b92b0>

Obviously, I should include some modules to use ActiveAdmin methods, but I can't find what exactly.

PS: I want to use currency_row :amount instead of:

row :amount do |deal|
  number_to_currency(deal.amount, precision: 0)
end

Upvotes: 0

Views: 998

Answers (1)

lorefnon
lorefnon

Reputation: 13105

This does not work because row method is not a helper and it comes from ActiveAdmin DSL.

While I would recommend against it, what you are trying to accomplish can be achieved by monkey patching the class ActiveAdmin::Views::AttributesTable which defines the row method.

You can alternatively create a helper and pass it self, using which row method can be accessed. However it will fail if used outside AttributesTable context.

I would recommend just using the last snippet of code you have written, which is IMHO sufficiently concise:

row :loan_amount do |deal|
  number_to_currency(deal.amount, precision: 0)
end

Upvotes: 3

Related Questions