67

I'm creating a helper to be used by Formtastic but I get the undefined local variable or method error. I don't know where to put it so it can work.

I already tried in the application_helper.rb and in app/helpers/active_admin/view_helpers.rb

leonel
  • 10,106
  • 21
  • 85
  • 129
  • 1
    To me it seems that the `ApplicationHelper` is automatically loaded, but I needed to restart my application for any new helpers to be detected by ActiveAdmin – Besi Mar 05 '18 at 12:23

10 Answers10

75

You can define them in app/helpers/ as you tried but you need to include them trough the active admin's initializer like this:

# in config/initializers/active_admin.rb
ActiveAdmin.setup do |config|
    ....
end

module ActiveAdmin::ViewHelpers
  include ApplicationHelper
end
Bishma Stornelli
  • 2,539
  • 4
  • 22
  • 30
  • This solution appears to work at first glance but ended up creating a conflict on the admin login page. I believe this conflict is because of Devise helpers from the user-facing application. @alony solution worked for me, though of course is not as dry if you are reusing helpers from your user-facing app. Perhaps the conflict will not occur if you include a helper other than ApplicationHelper with this method? – Ryan Francis Jun 19 '15 at 16:34
  • This still works in Rails 4.2 for me. No conflicts that I can find and I'm using Devise. But I don't have an admin login page - I have one user model for consumers and admins. – armchairdj Jun 22 '17 at 22:28
  • 1
    Worked for me with rails 5.1.6, active_admin 1.3.0 – Tony Vincent Mar 05 '19 at 16:16
67

You need to put your helper functions in app/helpers/active_admin/views_helper.rb file Example:

module ActiveAdmin::ViewsHelper #camelized file name
  def my_helper 
       # do something 
  end 
end 
okliv
  • 3,909
  • 30
  • 47
alony
  • 10,725
  • 3
  • 39
  • 46
  • 25
    One more tip: in 0.5.0 you should restart whole stack to get it work. – Dmitry Polushkin Sep 28 '12 at 14:01
  • 1
    Thanks Dmitry. I was about to give up and then I realized I had to restart :) – Venkat D. Oct 08 '12 at 21:53
  • 3
    @DmitryPolushkin, 1.0.0.pre. I ended up doing what bishma-stornelli suggested. At least it doesn't depend on some ethereal convention that AA developers can't resist changing regularly. – Gunchars Apr 02 '14 at 20:39
  • if you want to use this helper method also in collection action do this: `controller do include ActiveAdmin::ViewsHelper end` `collection_action :my_action do render json: my_helper end` – Lev Lukomsky Nov 19 '15 at 22:44
  • In ActiveAdmin 1.0.0.pre4, "module ActiveAdmin::ViewHelpers" did not work, but as explained in this answer, "module ActiveAdmin::ViewsHelper" did work. Also, I did have to restart the whole stack. Thank you @alony! – Chris Simeone Jul 15 '16 at 14:48
33

What I have found using ActiveAdmin 0.6.1 is that ActiveAdmin will look for helpers in app/helpers/active_admin/*_helper.rb, but the name doesn't really matter.

What does matter 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.

If anyone knows where this is officially documented, that would be awesome.

Here is an example: https://gist.github.com/afred/7035a657e8ec5ec08d3b

Avael Kross
  • 428
  • 1
  • 5
  • 17
Mars Redwyne
  • 1,267
  • 13
  • 8
12
app/helpers/active_admin/view_helpers.rb

didn't help me

EDITED: i changed it to views_helper.rb & ViewsHelper accordingly and it worked

*but if you want to define it only for certain resource, you can do it in my way


i had to define

#app/helpers/active_admin/categories_helper.rb

module ActiveAdmin::CategoriesHelper

  def helper_method

  end

end

for my active_admin resource app/admin/categories.rb

okliv
  • 3,909
  • 30
  • 47
7

Another way to do this is to make the specific ActiveAdmin controller generated behind-the-scenes include the helper. This method will allow making the inclusion of the helpers explicit per file rather than global.

ActiveAdmin.register MyModel do
  controller do
    include MyHelper
  end
end
Del
  • 397
  • 2
  • 9
  • This is really useful if you want to use some logic in both active admin and the rest of the application. – omnikron Jun 16 '17 at 14:20
5

I can make it work in ActiveAdmin 0.6.1 (finally!). The solution is to create a helper module as following:

# app/helpers/active_admin_helpers.rb
module ActiveAdminHelpers
  # make this method public (compulsory)
  def self.included(dsl)
    # nothing ...
  end

  # define helper methods here ...
  def helper_method
    ...
  end
end

then include this module this way:

# app/admin/[resource].rb
include ActiveAdminHelpers

ActiveAdmin.register [Resource] do
  ...

end

Actually, it's not a nice solution but it's DRY and working good. I have already read and tried a lot of methods and solutions such as ViewHelpers module (put under 'app/helpers' or 'app/admin/active_admin'), ActiveAdmin::DSL monkey patching, ... but those never worked in version 0.6.1 (I don't have any ideas about other versions) :(

Anh Nguyen
  • 1,202
  • 14
  • 17
  • 7
    This solution technically works, but it's not a good idea. You're actually including `ActiveAdminHelpers` in `Object`, at the top level. That means that once this file loads, those helper methods are available on *every object in the entire application*. – Peeja Mar 19 '14 at 14:59
  • 4
    In fact it's a terrible idea. Including helpers in the global scope will wreak havoc in unexpected and untrackable ways... I'm suffering it right now... :( – rewritten Apr 07 '16 at 10:45
  • 1
    I just went through and fixed an issue in our application where a developer had done a global include like that.. definitely not a good way to do things at all. – Urkle Aug 16 '17 at 21:16
4

Defining ActiveAdmin::ViewHelpers in app/admin/active_admin/view_helpers.rb works for me with activeadmin 0.3.4 and 0.5.0.

tfischbach
  • 3,003
  • 3
  • 22
  • 16
3

Using activeadmin 1.0.0.pre1 from git://github.com/activeadmin/activeadmin.git

Rails 4.2.1

This worked for me...

my_app/app/helpers/active_admin/resources_helper.rb

module ActiveAdmin
  module ResourcesHelper
    def resource_form_for(_resource, _params, _options = {}, &_block)
      url = if _resource.new_record?
              UrlBuilder.resources_path(_resource.class, _params)
            else
              UrlBuilder.resource_path(_resource.class, _params)
            end

      method = _resource.new_record? ? :post : :put

      options = { url: url, method: method, builder: ActiveAdmin::FormBuilder }
      options.merge!(_options)

      semantic_form_for([:admin, _resource], options) do |f|
        _block.call(f)
      end
    end
  end
end

my_app/app/admin/balance_sheets.rb

ActiveAdmin.register BalanceSheet do
  form partial: 'form'
end

my_app/app/views/admin/balance_sheets/_form.html.erb

<%= resource_form_for(resource, params) do |f| %>
  <%= f.inputs "Fields" do %>
    <%= f.input :progress_status %>
    <%= f.input :crew %>
    <%= f.input :shift %>
    <%= f.input :expected_progress %>
    <%= f.input :real_progress %>
    <%= f.input :analyst, collection: User.analysts %>
    <%= f.input :activity_ids, as: :check_boxes, collection: Activity.balance_sheet_activities %>
    <%= f.input :worker_ids, as: :check_boxes, collection: Worker.all %>
  <% end %>
  <%= f.actions %>
<% end %>
Leantraxxx
  • 4,506
  • 3
  • 38
  • 56
2

You can also use ActiveAdmin partials :

render partial: 'admin/my_partial', locals: { var: my_var }

And inside app/views/admin/_my_partial.html.arb your active_admin ruby code.

Luc Boissaye
  • 935
  • 8
  • 14
1

What worked for me with Rails 3.2.11 and and gem activeadmin (0.5.1) was not adding the app/active_admin/view_helpers.rb file, or declaring any modules in config/initializers/active_admin.rb

I put my helpers logically, by model, into the app/*_helpers.rb files. Then inside the app/admin/model.rb file I used:

# app/admin/[resource].rb
ActiveAdmin.register [Resource] do
  ...
  filter :gender, as: :select, collection: proc{genders}
  ...
end

To use the helper in filters, to display a drop down list of genders to filter on, in the list view. For the corresponding create form fields, I used:

# app/admin/[resource].rb
ActiveAdmin.register [Resource] do
  form do |f|
    f.inputs "Case Manager" do
      ...
      f.input :gender, as: :radio, collection: genders
      ...
      f.buttons
    end
  end
end

To display radio buttons for the input form.

Not sure why the proc{} is required outside of the form do |f| block, but if anyone can explain why it's a bad idea, I'll find a different way.

samsmith
  • 11
  • 4
  • Because the proc is making the code inside to be evaluated later. I think aa is loading the admin/* classes on boot and sometimes there are unmet dependencies. So with the proc you are telling the code to execute when everything is loaded. – Ivailo Bardarov Apr 11 '13 at 14:27