Activeadmin: the model of the parent resource page must be associated with a name equal to the name of the child resource page

the RegionCountry model must be associated with city names (since the child resource page generates a controller named CityController)

How do I customize the name of an association?

trace:

NoMethodError (undefined method `cities' for #<RegionCountry:0x0000000605f158>):
activemodel (4.0.9) lib/active_model/attribute_methods.rb:439:in `method_missing'
activerecord (4.0.9) lib/active_record/attribute_methods.rb:168:in `method_missing'
inherited_resources (1.5.1) lib/inherited_resources/base_helpers.rb:184:in `end_of_association_chain'

      

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
  navigation_menu :default
  filter :id_eq
end

      

r / 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 :reg_cities, class_name: RegionCity, foreign_key: :country_id
end

      

Gemfile.lock

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

      

but if we changed app / models / region_city.rb like this:

class RegionCountry < ActiveRecord::Base
  validates :name, presence: true
  has_many :cities, class_name: RegionCity, foreign_key: :country_id # this line was changed
end

      

it works fine

here is a test app that repeats the error: https://github.com/senid231/activeadmin_test_belongs_to/tree/rename_child

+3


source to share


1 answer


By default, Inherited Resources (used by ActiveAdmin) involves collecting the name to mirror the resource name, which will ResourceCountry#cities

and explain the undefined method error. Changing the default values ​​in the controller will fix the problem:

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

  controller do

    # Instruct inherited_resources on how to find the collection from the
    # parent resource (by default it is assumed to be `:cities`); instead
    # use `:reg_cities` as the name of the collection (from
    # `RegionCountry#reg_cities`):

    defaults collection_name: :reg_cities
  end
end

      



See the inherited resource documentation for overriding default settings for more information. detail.

+2


source







All Articles