![](/img/trans.png)
[英]rails4-autocomplete gem with active_admin and belongs_to relation
[英]optional belongs_to for renamed active_admin resources
如果belongs_to是可選的,則使用polymorphic_path和polymorphic_link方法創建URL輔助程序。 此方法對活動admin中的資源重命名一無所知。
如何將可選的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'
應用程序/管理/ 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
應用程序/管理/ 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
應用程序/模型/ 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控制器如何從父資源訪問子資源。 因此,您的子類最終將看起來像這樣:
ActiveAdmin.register RegionCity, as: 'City' do
belongs_to :country
...snipped....
controller do
defaults :collection_name => "region_cities"
end
end
因為ActiveAdmin使用繼承資源作為其動力來源,所以在繼承資源文檔中可以找到除我的幫助之外您可能需要進行的其他任何調整。 此類具體示例在“ 覆蓋默認值”部分中。 當前不維護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
並正確顯示。
路徑和URL幫助程序已更改,因此我一直在使用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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.