gpt4 book ai didi

ruby-on-rails - 重命名的 active_admin 资源的可选 belongs_to

转载 作者:数据小太阳 更新时间:2023-10-29 08:46:14 28 4
gpt4 key购买 nike

如果 belongs_to 是可选的,则使用 polymorphic_path 和 polymorphic_link 方法创建 url 助手。此方法对事件管理员中的资源重命名一无所知。

如何为重命名的资源页面使用可选的 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

应用/模型/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

重复错误的小型测试应用:

https://github.com/senid231/activeadmin_test_belongs_to

最佳答案

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 Controller 如何从父资源访问子资源。因此,您的子类最终会看起来像这样:

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

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

因为 ActiveAdmin 使用 Inherited Resources作为其强大的来源,除了我的帮助之外,您可能需要的任何其他调整都可以在继承资源文档中找到。这种例子具体在 Overwriting Defaults部分。 IR 当前未得到维护,所以不要对所有 Rails 3 引用资料感到惊讶。

希望这对您有所帮助!

关于ruby-on-rails - 重命名的 active_admin 资源的可选 belongs_to,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27904768/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com