custom actions inside rails_admin gem
Asked Answered
T

2

5

I don't understand very good yet how it works rails_admin gem for create a new action.

I want create a action with name balance in rails_admin root.

I have created a file with name rails_admin_balance.rb inside myapp/lib folder like:

require 'rails_admin/config/actions'
require 'rails_admin/config/actions/base'

module RailsAdminBalance
end

module RailsAdmin
  module Config
    module Actions
      class Balance < RailsAdmin::Config::Actions::Base
       RailsAdmin::Config::Actions.register(self)
        register_instance_option :object_level do
          true
        end
        register_instance_option :link_icon do
         'icon-eye-open'
        end
        register_instance_option :root? do
          true
        end
      end
    end
  end
end

I have created a new file on /views/rails_admin/main/balance.html.erb

inside my rails_admin.rb file I have

config.actions do
 dashboard                     # mandatory
 # collection actions 
 index                         # mandatory
 new
 export
 history_index
 bulk_delete
 # member actions
 show
 edit
 delete
 history_show
 show_in_app
 balance 
end

When I try run rails server I get this error:

Exiting

/home/ubuntu/Desktop/myapp/config/initializers/rails_admin.rb:33:in `block (2 levels) in <top (required)>': undefined method `balance' for RailsAdmin::Config::Actions:Module (NoMethodError)....

Where have I the error?

What am I doing bad?

Thank you very much!

Thomasthomasa answered 23/10, 2012 at 13:40 Comment(0)
E
9

I've had the exact same problem. I think it's because the rails_admin_[my_action].rb file under /lib doesn't get loaded, and this is not documented in the rails_admin docs.

You can either try to load it from rails_admin.rb or just move all the code you have onrails_admin_[my_action].rbto therails_admin.rb` initializer (paste it at the beginning of the file).

There is a third option (only if you don't want to reuse the action) which IMHO looks cleaner:

Get rid of the rails_admin_[my_action].rb (your rails_admin_balance.rb) and rewrite your actions inside rails_admin.rb as follows:

config.actions do
  dashboard                     # mandatory
  # collection actions 
  index                         # mandatory
  new
  export
  history_index
  bulk_delete
  # member actions
  show
  edit
  delete
  history_show
  show_in_app

  member :balance do
    link_icon 'icon-eye-open'
    root? true
  end
end
Exultation answered 2/11, 2012 at 12:15 Comment(1)
what would you do to enable this custom action only for specific models?Skinny
C
5

Just add this on your rails_admin.rb

module RailsAdmin
  module Config
    module Actions
      class Balance < RailsAdmin::Config::Actions::Base
        RailsAdmin::Config::Actions.register(self)
      end
    end
  end
end

Opening the Actions module on RailsAdmin scope avoids this particular error. And your definitions can stay on Balance module on lib.

Cosmonaut answered 1/5, 2014 at 12:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.