Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

custom actions inside rails_admin gem

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!

like image 765
hyperrjas Avatar asked Oct 23 '12 13:10

hyperrjas


2 Answers

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
like image 135
jävi Avatar answered Nov 10 '22 01:11

jävi


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.

like image 25
Oswaldo Ferreira Avatar answered Nov 10 '22 00:11

Oswaldo Ferreira