Skip to content

Instantly share code, notes, and snippets.

@afred
Last active March 25, 2024 21:30
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save afred/7035a657e8ec5ec08d3b to your computer and use it in GitHub Desktop.
Save afred/7035a657e8ec5ec08d3b to your computer and use it in GitHub Desktop.
Example using of ActiveAdmin helpers

What matters is:

  1. the filename must end in "_helper.rb"
  2. the module name must be the camel-case of the file name
  3. the file must be in app/helpers/active_admin/ directory.

the first helper:

# app/helpers/active_admin/foo_helper.rb
module ActiveAdmin::FooHelper
  def foo
    "i pity the foo!!!"
  end
end

the second helper:

# app/helpers/active_admin/bar_helper.rb
module ActiveAdmin::BarHelper
  def bar
    "i raise the bar!!!"
  end
end

and methods from both are available in view methods across models like so:

# app/admin/users.rb
ActiveAdmin.register User do
  #...
  show do |user|
    row "Test Both Helper" do |user|
      # both helper methods work here
      foo
      bar
    end
  end
  #...
end

and in the view method for a different model...

# app/admin/company.rb
ActiveAdmin.register Company do
  # ...
  show do |company|
    row "Test Both Helpers" do |company|
      # both helper methods available here too
      foo
      bar
    end
  end
  # ...
end
@IslamAzab
Copy link

Should the helper be required or something?

I can not use the 'panel' helper.

Thanks a lot!

@pavan-kumarv
Copy link

The helper does not work outside of show, index or form

@skaidra-bake
Copy link

skaidra-bake commented Mar 25, 2024

a pro tip:
make sure to restart the server if your error persists after making these changes 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment