Vitali
Vitali

Reputation: 710

Rails - undefined method `find' for nil:NilClass (ActiveAdmin)

Say i got 2 controllers.

advertiser_campaign and advertiser_ad.

I need advertiser_ad to be a child of advertiser_campaign but as i am fairly new to activeadmin (and ruby itself) i am having some problems. I am trying to do a nested resource using ActiveAdmin.

Code below indeed registers required routes

ActiveAdmin.register Advertiser::Ad do
  belongs_to :advertiser_campaign
end

BUT when i try to access /admin/advertiser_campaigns/1/advertiser_ads I get error

NoMethodError in Admin::AdvertiserAdsController#index
undefined method `find' for nil:NilClass

Which probably means that routes work quite well and problem is somewhere in controller. I tried to overwrite ActiveAdmin controller by doing something like this

controller do
  def index
    index! do |format|
      @advertiser_ads = Advertiser::Ad.all
      format.html
    end
  end
end

But there are absolutely no effect. Although i see that controller attempts to execute my customly overwritten controller but encounters same error.

In my advertiser_campaign model i have

has_many :advertiser_ads, :class_name => 'Advertiser::Ad'

and in my advertiser_ad model

belongs_to :advertiser_campaign, :class_name => 'Advertiser::Campaign'

Any help regarding this issue will be highly appreciated. Thank you in advance.

Upvotes: 0

Views: 2421

Answers (1)

oldhomemovie
oldhomemovie

Reputation: 15129

The docs says that any resource is registered under a namespace. From your example it looks that you're trying to make use of Advertiser::Ad class without explicitly specifying a namespace. Try:

AdminAdmin::Advertiser::Ad

Anyway, if that doesn't help, use rails console (check for registered namespaces/classes) to quicker figure out the root of the problem.

Upvotes: 1

Related Questions