Link 在活动管理员中自定义多态关联操作
Link to custom polymorphic association action in active admin
我正在与 Active Admin 合作。目前,我有一个索引页面,允许我查看所有已被用户标记的资源。鉴于存在各种类型的资源,我的 Flagged class 和各种资源表之间存在多态关系。
在我的索引页面中,在 Active Admin 上,我希望能够 link 到属于特定资源类型的适当显示页面。
我的所有代码都可以正常工作,但我不喜欢我提出的解决方案。
如何改进我当前的解决方案,这样我就不必写一个笨拙的条件?
型号
class Flag < ActiveRecord::Base
#create relationships with user and flag model
belongs_to :flaggable, polymorphic: true
end
class MilitaryResource < ActiveRecord::Base
has_many :flags, as: :flaggable, :dependent => :destroy
end
class DistrictResource < ActiveRecord::Base
has_many :flags, as: :flaggable, :dependent => :destroy
end
class SchoolResource < ActiveRecord::Base
has_many :flags, as: :flaggable, :dependent => :destroy
end
ActiveAdmin
ActiveAdmin.register Flag do
#parameters permitted to create military resources
permit_params :id, :user_id, :flaggable_id, :flaggable_type, :message, :created_at
#index page
index do
column :flaggable_type
###Current Solution
column "Flagged Resource" do |site|
if site.flaggable_type == "MilitaryResource"
link_to site.flaggable.name, :controller => "military_resources", :action => "show", :id => "#{site.flaggable_id}".html_safe
elsif site.flaggable_type == "SchoolResource"
link_to site.flaggable.name, :controller => "school_resources", :action => "show", :id => "#{site.flaggable_id}".html_safe
elsif site.flaggable_type == "DistrictResource"
link_to site.flaggable.name, :controller => "district_resources", :action => "show", :id => "#{site.flaggable_id}".html_safe
end
end
column :message
actions
end
你可以这样做来避免条件:
column "Flagged Resource" do |site|
link_to site.flaggable.name,
:controller => site.flaggable_type.underscore.pluralize,
:action => "show",
:id => "#{site.flaggable_id}"
.html_safe
end
我正在与 Active Admin 合作。目前,我有一个索引页面,允许我查看所有已被用户标记的资源。鉴于存在各种类型的资源,我的 Flagged class 和各种资源表之间存在多态关系。
在我的索引页面中,在 Active Admin 上,我希望能够 link 到属于特定资源类型的适当显示页面。
我的所有代码都可以正常工作,但我不喜欢我提出的解决方案。
如何改进我当前的解决方案,这样我就不必写一个笨拙的条件?
型号
class Flag < ActiveRecord::Base
#create relationships with user and flag model
belongs_to :flaggable, polymorphic: true
end
class MilitaryResource < ActiveRecord::Base
has_many :flags, as: :flaggable, :dependent => :destroy
end
class DistrictResource < ActiveRecord::Base
has_many :flags, as: :flaggable, :dependent => :destroy
end
class SchoolResource < ActiveRecord::Base
has_many :flags, as: :flaggable, :dependent => :destroy
end
ActiveAdmin
ActiveAdmin.register Flag do
#parameters permitted to create military resources
permit_params :id, :user_id, :flaggable_id, :flaggable_type, :message, :created_at
#index page
index do
column :flaggable_type
###Current Solution
column "Flagged Resource" do |site|
if site.flaggable_type == "MilitaryResource"
link_to site.flaggable.name, :controller => "military_resources", :action => "show", :id => "#{site.flaggable_id}".html_safe
elsif site.flaggable_type == "SchoolResource"
link_to site.flaggable.name, :controller => "school_resources", :action => "show", :id => "#{site.flaggable_id}".html_safe
elsif site.flaggable_type == "DistrictResource"
link_to site.flaggable.name, :controller => "district_resources", :action => "show", :id => "#{site.flaggable_id}".html_safe
end
end
column :message
actions
end
你可以这样做来避免条件:
column "Flagged Resource" do |site|
link_to site.flaggable.name,
:controller => site.flaggable_type.underscore.pluralize,
:action => "show",
:id => "#{site.flaggable_id}"
.html_safe
end