Rails 3. How to add a helper that will use ActiveAdmin?

I create a helper that Formtastic will use, but I get an undefined local variable or method error. I don’t know where to put it so that it can work.

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

+55
ruby ruby-on-rails helpers activeadmin
Dec 29 '11 at 20:35
source share
10 answers

You can define them in the application / helpers / as you tried, but you need to enable them through the active admin initializer as follows:

 # in config/initializers/active_admin.rb ActiveAdmin.setup do |config| .... end module ActiveAdmin::ViewHelpers include ApplicationHelper end 
+62
Oct 30 '12 at 13:39
source share

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

 module ActiveAdmin::ViewsHelper #camelized file name def my_helper # do something end end 
+55
Feb 06 2018-12-12T00:
source share

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

What matters:

  • the file name must end with "_helper.rb"
  • module name must be camel case file name
  • the file should be in the 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

+21
Dec 20 '13 at 21:41
source share
 app/helpers/active_admin/view_helpers.rb 

didn't help me

EDITED: I changed it to views_helper.rb and ViewsHelper respectively, and it worked

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




I needed to identify

 #app/helpers/active_admin/categories_helper.rb module ActiveAdmin::CategoriesHelper def helper_method end end 

for my resource active_admin app / admin / categories.rb

+10
May 7 '12 at 21:06
source share

I can get it to work in ActiveAdmin 0.6.1 (finally!). The solution is to create an auxiliary module as follows:

 # 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 enable this module as follows:

 # app/admin/[resource].rb include ActiveAdminHelpers ActiveAdmin.register [Resource] do ... end 

Actually, this is not a good solution, but it is DRY and works well. I have already read and tried many methods and solutions, such as the ViewHelpers module (placed under "app / helpers" or "app / admin / active_admin"), ActiveAdmin :: DSL monkey patch, but those that never worked in version 0.6 .1 (I have no idea about other versions): (

+6
Nov 21 '13 at 7:49 on
source share

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

+4
Feb 17 2018-12-12T00:
source share

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

Rails 4.2.1

It 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 %> 
+3
Nov 12 '15 at
source share

You can also use ActiveAdmin scores:

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

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

+2
Feb 26 '15 at 8:56
source share

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

I placed my helpers logically, by model, in the app / * _ helpers.rb files. Then in 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 auxiliary filter in the filters, to display a drop-down list of gender groups for filtering in the list. For the corresponding fields of the form form, 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 

Display of radio buttons for input form.

I don’t know why proc{} is required outside the form do |f| block form do |f| but if anyone can explain why this is a bad idea, I will find another way.

+1
Feb 10 '13 at 16:12
source share

Another way to do this is to include an assistant in the special ActiveAdmin controller created behind the scenes. This method will make the inclusion of helpers explicit to the file, not global.

 ActiveAdmin.register MyModel do controller do include MyHelper end end 
+1
06 Feb '17 at 17:19
source share