activeadmin: belongs_to and undefined method `find' for nil:NilClass

I’ve a HotelPicture model belongs_to a Hotel model.


ActiveAdmin.register HotelPicture do
  config.comments = false
  menu :parent => "Hotels"

  belongs_to :hotel
end

If I check routes I’ve :

batch_action_admin_hotel_hotel_pictures POST       /admin/hotels/:hotel_id/hotel_pictures/batch_action(.:format) admin/hotel_pictures#batch_action
              admin_hotel_hotel_pictures GET        /admin/hotels/:hotel_id/hotel_pictures(.:format)              admin/hotel_pictures#index
                                         POST       /admin/hotels/:hotel_id/hotel_pictures(.:format)              admin/hotel_pictures#create
           new_admin_hotel_hotel_picture GET        /admin/hotels/:hotel_id/hotel_pictures/new(.:format)          admin/hotel_pictures#new
          edit_admin_hotel_hotel_picture GET        /admin/hotels/:hotel_id/hotel_pictures/:id/edit(.:format)     admin/hotel_pictures#edit
               admin_hotel_hotel_picture GET        /admin/hotels/:hotel_id/hotel_pictures/:id(.:format)          admin/hotel_pictures#show
                                         PATCH      /admin/hotels/:hotel_id/hotel_pictures/:id(.:format)          admin/hotel_pictures#update
                                         PUT        /admin/hotels/:hotel_id/hotel_pictures/:id(.:format)          admin/hotel_pictures#update
                                         DELETE     /admin/hotels/:hotel_id/hotel_pictures/:id(.:format)          admin/hotel_pictures#destroy

The link generated with admin_hotel_hotel_pictures_path(hotel) is corect. But when I try to get http://0.0.0.0:3000/admin/hotels/852/hotel_pictures I get a NoMethodError : undefined method `find’ for nil:NilClass

it look like the problem come from inherited_resources gem.

Admin::HotelPicturesController#evaluate_parent
line 90 of belongs_to_helpers.rb : parent = parent.send(parent_config[:finder], params[parent_config[:param]])

I’m using ruby 2.0.0-p247 & rails 4.0.0 is anybody encountered the same kind of problem?

thank you

About this issue

  • Original URL
  • State: closed
  • Created 11 years ago
  • Comments: 26 (7 by maintainers)

Most upvoted comments

This will require additional research, but within inherited_resource the parent class is nil. So specifying the :parent_class on the belongs_to fixes the issue. See inherited_resources/belongs_to_helpers.rb#L87

ActiveAdmin.register HotelPicture do
  belongs_to :hotel, parent_class: Hotel # <= Specify the parent_class

  controller do
    private
    def permitted_params
      params.permit(:hotel_picture => [:hotel_id, :picture_id, :position])
    end
  end
end

I would expect inherited_resources to assume the parent class from belongs_to :hotel as being the Hotel class without additional configuration. This may be a good enhancement for inherited_resources.

@MrMoins thanks for providing an example app!