可选 belongs_to 重命名的 active_admin 资源
optional belongs_to for renamed active_admin resources
如果 belongs_to 是可选的 url 助手是使用 polymorphic_path 和 polymorphic_link 方法创建的。
此方法对活动管理员中的资源重命名一无所知。
如何使用可选 belongs_to 重命名资源页面
回溯:
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
重复错误的小型测试应用程序:
ActiveAdmin 允许您通过 belongs_to
方法使用嵌套资源,您显然已经知道:
ActiveAdmin.register Project do
end
ActiveAdmin.register Ticket do
belongs_to :project
end
ActiveAdmin.register Milestone do
belongs_to :project
end
因为 Inherited Resources 的火力无法识别书中的每个自定义实现,您可能必须明确告诉您的 ActiveAdmin 控制器如何从 parent 资源访问 child 资源.所以,你的 child 类 最终会看起来像这样:
ActiveAdmin.register RegionCity, as: 'City' do
belongs_to :country
...snipped....
controller do
defaults :collection_name => "region_cities"
end
end
因为 ActiveAdmin 使用 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 部分。 IR 目前没有维护,所以不要对所有 Rails 3 个参考感到惊讶。
希望对您有所帮助!
感谢 Colin Graves 的上述回答,非常有用。
我有一个问题,父资源是重命名的东西,这是我必须做的:
ActiveAdmin.register Project, as 'Enterprise' do
controller do
defaults collection_name: 'projects', instance_name: 'project'
end
end
ActiveAdmin.register Ticket do
belongs_to :project, param: 'enterprise_id', route_name: 'enterprise'
end
defaults collection_name: 'projects', instance_name: 'project'
意味着我不必将现有代码中的所有 projects
或 project
更改为 enterprises
或 enterprise
。
传递给 belongs_to
的选项使 ticket
视图能够找到它们的 project
并正确显示。
Path & url helpers 改变了,所以我一直在使用 admin_projects_path
的任何地方我不得不用 admin_enterprises_path
等替换它
我发现有助于发现可用选项的一些继承资源代码是 https://github.com/josevalim/inherited_resources/blob/master/lib/inherited_resources/belongs_to_helpers.rb & https://github.com/josevalim/inherited_resources/blob/master/lib/inherited_resources/class_methods.rb
如果 belongs_to 是可选的 url 助手是使用 polymorphic_path 和 polymorphic_link 方法创建的。 此方法对活动管理员中的资源重命名一无所知。
如何使用可选 belongs_to 重命名资源页面
回溯:
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
重复错误的小型测试应用程序:
ActiveAdmin 允许您通过 belongs_to
方法使用嵌套资源,您显然已经知道:
ActiveAdmin.register Project do
end
ActiveAdmin.register Ticket do
belongs_to :project
end
ActiveAdmin.register Milestone do
belongs_to :project
end
因为 Inherited Resources 的火力无法识别书中的每个自定义实现,您可能必须明确告诉您的 ActiveAdmin 控制器如何从 parent 资源访问 child 资源.所以,你的 child 类 最终会看起来像这样:
ActiveAdmin.register RegionCity, as: 'City' do
belongs_to :country
...snipped....
controller do
defaults :collection_name => "region_cities"
end
end
因为 ActiveAdmin 使用 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 部分。 IR 目前没有维护,所以不要对所有 Rails 3 个参考感到惊讶。
希望对您有所帮助!
感谢 Colin Graves 的上述回答,非常有用。
我有一个问题,父资源是重命名的东西,这是我必须做的:
ActiveAdmin.register Project, as 'Enterprise' do
controller do
defaults collection_name: 'projects', instance_name: 'project'
end
end
ActiveAdmin.register Ticket do
belongs_to :project, param: 'enterprise_id', route_name: 'enterprise'
end
defaults collection_name: 'projects', instance_name: 'project'
意味着我不必将现有代码中的所有 projects
或 project
更改为 enterprises
或 enterprise
。
传递给 belongs_to
的选项使 ticket
视图能够找到它们的 project
并正确显示。
Path & url helpers 改变了,所以我一直在使用 admin_projects_path
的任何地方我不得不用 admin_enterprises_path
等替换它
我发现有助于发现可用选项的一些继承资源代码是 https://github.com/josevalim/inherited_resources/blob/master/lib/inherited_resources/belongs_to_helpers.rb & https://github.com/josevalim/inherited_resources/blob/master/lib/inherited_resources/class_methods.rb