Dan Tappin
Dan Tappin

Reputation: 3032

ActiveAdmin after_create: callback error - wrong number of arguments (1 for 0)

I have an ActiveAdmin page that created a record in my rails app. The rails app only views / updates this model and admin page only creates and destroys.

I am trying to add a custom method to call back after creation. It goes something like this (from what I found on a few google searches):

    ActiveAdmin.register Company do

      controller do 

        def add_owner

         ## My code etc  

        end

      end

    end

    after_create :add_owner

I am adding it here because I am using some parameters passed from the AA form that are not in my company model. I am passing an email address and a few other fields with the intent that I create a new user for the new company etc. they get sent an email etc. and so on. The issue is as soon as I try this I get:

ArgumentError at /admin/companies
wrong number of arguments (1 for 0)

My stack end off like this:

ArgumentError - wrong number of arguments (1 for 0):
   () Users/myapp/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/bundler/gems/activeadmin-1b5c9259c4cf/lib/active_admin/callbacks.rb:17:in `run_callback'
   () Users/myapp/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/bundler/gems/activeadmin-1b5c9259c4cf/lib/active_admin/callbacks.rb:80:in `block (3 levels) in define_active_admin_callbacks'
   () Users/myapp/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/bundler/gems/activeadmin-1b5c9259c4cf/lib/active_admin/callbacks.rb:80:in `block (2 levels) in define_active_admin_callbacks'
   () Users/myapp/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/bundler/gems/activeadmin-1b5c9259c4cf/lib/active_admin/resource_controller/data_access.rb:148:in `create_resource'
  inherited_resources (1.6.0) lib/inherited_resources/actions.rb:33:in `create'

Am I just doing this the wrong way?

On a side note on my better errors page I have this:

Local Variables

method  
:add_owner
args    
[#<Company id: nil, name: "Test", name_short: "Test", address1: "Test", address2: "", city: "Test", province: "Test", postal: "Test", phone: nil, fax: "", url: "", user_id: nil, theme: nil, flag_active: true, flag_free: true, tenant_name: nil, twitter: "", facebook: "", linkedin: "", created_at: nil, updated_at: nil>]

Is AA passing my to be created new Company to the callback as an argument? Hence the wrong number of arguments (1 for 0)?

Upvotes: 1

Views: 2586

Answers (1)

Dan Tappin
Dan Tappin

Reputation: 3032

I ended up doing this:

controller do

  after_create do

    owner = User.find_or_create_by(email: params[:company][:email])
    owner.enrollments.new(company_id: @company.id, roles: ['owner'], title: params[:company][:title])
    owner.save(validate: false)
    owner.invite!(owner)

  end

end

Its easy and it works.

Upvotes: 2

Related Questions