I have polymorphic models:
class Picture < ActiveRecord::Base
belongs_to :imageable, :polymorphic => true
end
class Service < ActiveRecord::Base
has_many :pictures, :as => :imageable
end
class Product < ActiveRecord::Base
has_many :pictures, :as => :imageable
end
In order to get my activeadmin model to work with both parents (Service and Product) I need to do something like:
ActiveAdmin.register Picture do
def who_do_i_belong_to?
uri = how_to_get_uri?
if uri.match(/products/)
:product
else
:service
end
end
belongs_to who_do_i_belong_to?
end
The workaround seems to work. I only miss how to get the url/uri from inside the who_do_i_belong_to? method.
controller.controller_name # "admin/services", so it is not useful.
Thank you in advanced.
If you want to have CRUD for your polymorphic nested resources (
products/picturesandservices/pictures), your application needs to have routes like/admin/products/:id/imagesand/admin/services/:id/images. The problem is that when you usebelongs_to :parentin aregisterblock, active_admin will only generate one nested routeadmin/parents/:id/child, whereas you need two. Furthermore,:parentcan’t be determine by the current url, because the callbelongs_to :parentitself is used to create the current url (the resource path).To get around this, you can define the routes yourself in configs.rb
and tell active_admin to use these routes by writing
controller.belongs_to :service, :product, polymorphic: truein yourregisterblock forPicture.Source: https://github.com/gregbell/active_admin/issues/1183