Senid Senid - 4 months ago 35
Ruby Question

optional belongs_to for renamed active_admin resources

if belongs_to is optional url helpers are created with use of polymorphic_path and polymorphic_link methods.
This methods don't know anything about resource renaming in active admin.

How can I use optional belongs_to for renamed resource pages

backtrace:

ActionView::Template::Error (undefined method `new_admin_region_country_region_city_path' for #<Admin::CitiesController:0x00000006bb1dd0>):
1: insert_tag renderer_for(:index)
actionpack (4.0.9) lib/action_dispatch/routing/polymorphic_routes.rb:129:in `polymorphic_url'
actionpack (4.0.9) lib/action_dispatch/routing/polymorphic_routes.rb:147:in `new_polymorphic_path'
inherited_resources (1.5.1) lib/inherited_resources/url_helpers.rb:222:in `new_resource_path'
actionpack (4.0.9) lib/abstract_controller/helpers.rb:53:in `new_resource_path'
arbre (1.0.2) lib/arbre/element.rb:180:in `method_missing'
/home/senid/.rvm/gems/ruby-2.1.2@belongs_to/bundler/gems/active_admin-a2cd9604c2d9/lib/active_admin/resource/action_items.rb:61:in `block in add_default_action_items'
/home/senid/.rvm/gems/ruby-2.1.2@belongs_to/bundler/gems/active_admin-a2cd9604c2d9/lib/active_admin/views/action_items.rb:9:in `instance_exec'
/home/senid/.rvm/gems/ruby-2.1.2@belongs_to/bundler/gems/active_admin-a2cd9604c2d9/lib/active_admin/views/action_items.rb:9:in `block (2 levels) in build'


app/admin/region_city.rb

ActiveAdmin.register RegionCity, as: 'City' do
permit_params :name, :description, :country_id
menu false
belongs_to :country, parent_class: RegionCountry, optional: true
navigation_menu :default
filter :id_eq
end


app/admin/region_country.rb

ActiveAdmin.register RegionCountry, as: 'Country' do
permit_params :name, :description

filter :id_eq

sidebar 'Links', only: [:show] do
ul do
li do
link_to 'Cities', admin_country_cities_path(country_id: resource.id)
end
end
end

end


app/models/region_city.rb

class RegionCity < ActiveRecord::Base
belongs_to :country, class_name: RegionCountry, foreign_key: :country_id
validates :name, presence: true
validates :country, presence: true
end


app/models/region_country.rb

class RegionCountry < ActiveRecord::Base
validates :name, presence: true
has_many :cities, class_name: RegionCity, foreign_key: :country_id
end


Gemfile.lock

GIT
remote: git://github.com/gregbell/active_admin.git
revision: a2cd9604c2d949f5193791045385756cee0c6865


small test app that repeats an error:

https://github.com/senid231/activeadmin_test_belongs_to

Answer

ActiveAdmin allows you to use nested resources with a belongs_to method, as you clearly already know:

ActiveAdmin.register Project do 
end

ActiveAdmin.register Ticket do
  belongs_to :project
end

ActiveAdmin.register Milestone do 
  belongs_to :project
end

Because Inherited Resources' firepower can't be aware of every custom implementation in the book, you may have to explicitly tell your ActiveAdmin controller how to access the child resources from the parent resource. So, your child classes would end up looking something like this:

ActiveAdmin.register RegionCity, as: 'City' do
  belongs_to :country
  ...snipped....

  controller do 
    defaults :collection_name => "region_cities"
  end
end

Because ActiveAdmin uses Inherited Resources as its source of power, any additional tweaking you may need in addition to my help will be found in the Inherited Resources documentation. This sort of example in specific is in the Overwriting Defaults section. IR isn't currently being maintained, so don't be surprised by all the Rails 3 references.

Hope this helps!