Rails,Polymorphic Association – 仅渲染关联实例

我正在尝试学习如何在我的Rails 5应用程序中使用多态关联。

我有一个名为Organization,Proposal和Package :: Bip的模型。

协会是:

组织

has_many :bips, as: :ipable, class_name: Package::Bip accepts_nested_attributes_for :bips, reject_if: :all_blank, allow_destroy: true 

提案

 has_many :bips, as: :ipable, class_name: Package::Bip accepts_nested_attributes_for :bips, reject_if: :all_blank, allow_destroy: true 

套票:必必

 belongs_to :ipable, :polymorphic => true, optional: true, inverse_of: :bip 

Package :: Bip可以与Organization或Proposal相关联。 我正在努力弄清楚如何在我的提案节目中展示仅属于提案的Package :: Bips,以及组织中的提案。

我的package :: bip表有以下两列:

 # ipable_id :integer # ipable_type :string 

类型设置为Proposal或Organization。

在我的提案节目中,我有:

    

我认为(@ proposal)应该是决定返回哪个package_bip实例的约束因素。 但事实并非如此。 我不确定它在做什么,因为所有package_bips都被渲染(包括与组织相关的那些)。

在我的提案控制器中,我有:

  def show @images = @proposal.images @bips = @proposal.bips.where(ipable_type: 'Proposal') end 

我添加了上面的范围,试图强制show只使用在ipable_type属性中设置Proposal的那些 – 但是这并没有做任何帮助。

我不确定我是否应该在Package :: Bips控制器中创建一个if语句来查看ipable_type是否是提案。 我不明白如何exctract多态对象的关联实例,以便我可以在提案显示页面的索引中显示相关的实例(索引仅填充属于提案的实例)。

我尝试将Package :: Bips控制器的索引操作编写为:

 def index if params[:ipable_type] @bips = Package::Bip.where(ipable_type: 'Proposal') elsif params[:ipable_type] @bips = Package::Bip.where(ipable_type: 'Organisation') else @bips = Package::Bip.all end # authorize @bips end 

那什么都没做。 所以我不确定为什么它至少不会给出错误信息。

渲染右视图

在我的提案/节目中 – 我有:

  

该链接转到我的views / package / bips / index.html。 该观点有:

  . 

这是目前列出所有的bips(组织和提案)。 我认为提案/节目中链接中的@proposal可能会限制事物,或者我的bips控制器中的索引操作方法可能(上图)可能会有所帮助。 但这些都不会过滤实例。

如果我写:

   

在我的提案显示视图中,它可以渲染标题。

现在我的挑战是如何让双向索引视图呈现正确的实例集(仅提议或仅组织),具体取决于发送请求以呈现该视图的页面。 也许我可以编写请求引用来查看它的组织控制器或提议控制器是否请求?

尝试渲染视图

意见/建议/ show.html.erb

   
Intellectual Property

视图/ BIPS / index.html.erb

     

视图/ BIPS / show.html.erb

 

提案管制员

 def index @proposals = Proposal.all # @bips = @proposal.bips # authorize @proposals end def show @images = @proposal.images @bips = @proposal.bips#.where(ipable_type: 'Proposal') end 

bips控制器

 def index # if params[:status] # @bips = Package::Bip.where(:status => params[:status]) # else # @bips = Package::Bip.all # end if params[:ipable_type] @bips = Package::Bip.where(ipable_type: 'Proposal') elsif params[:ipable_type] @bips = Package::Bip.where(ipable_type: 'Organisation') else @bips = Package::Bip.all end # authorize @bips end 

新的尝试

我找到了这个:

http://rails-bestpractices.com/posts/2010/09/23/generate-polymorphic-url/

我不明白文章中使用的“父”是否是某种关键字,或者我是否需要在某处定义它。

我试图嵌套我的路线,所以现在:

 resources :proposals do namespace :package do resources :materials resources :insights resources :facilities resources :participants resources :fundings resources :facts resources :bips end 

……对于组织而言也是如此。

但是,我收到了错误。 我想也许这篇文章已经向前迈了几步。 是否有使用多态路径的约定?

我现在找到了这个资源:

http://www.codequizzes.com/rails/learn-rails/polymorphism

创建动作对我来说很不寻常。 任何人都可以解释这种方法中使用的创建动作概念吗?

下一步尝试

Package :: Bips控制器中的新索引操作:

 def index if Package::Bip.ipable_type: 'Proposal' @bips = Package::Bip.where(ipable_type: 'Proposal') elsif Package::Bip.ipable_type: 'Organisation' @bips = Package::Bip.where(ipable_type: 'Organisation') else @bips = Package::Bip.all end 

但现在,仍然没有让我试着看看它是否有效。

当我尝试为提案呈现展示视图时,我收到一条错误消息:

 undefined method `package_bips_path' for #<#:0x007fa72ea93ee0> 

这是显示页面包含的链接:

   
Intellectual Property

另一个去吧

bips控制器,索引动作:

  if params[:ipable_type] == 'Proposal' @bips = Package::Bip.where(ipable_type: 'Proposal') else params[:ipable_type] == 'Organisation' @bips = Package::Bip.where(ipable_type: 'Organisation') # else # @bips = Package::Bip.all end 

提案显示:

     

视图/ BIPS /索引

 

Intellectual Property Assets

· "Offered") %> · "Sought") %>
Title
Asset
Added
Manage this asset
· <!-- · -->

这不起作用(仍然呈现错误的bips),但除了不正确之外,我在显示页面上没有任何链接。 如果我想链接到:bips show view上的status params,它从一个没有提案/组织前缀的路径开始 – 所以它不起作用。

下一步尝试

bips控制器索引操作现在具有:

  if params[:ipable_type] == "Proposal" @bips = Package::Bip.where(:ipable_type == 'Proposal') else params[:ipable_type] == 'Organisation' @bips = Package::Bip.where(:ipable_type == 'Organisation') 

现在这会呈现bips,但它会在组织索引(以及提案索引)中呈现提案bips。

我被卡住了!

在控制台中,我可以这样做:

 p = Proposal.last Proposal Load (6.1ms) SELECT "proposals".* FROM "proposals" ORDER BY "proposals"."id" DESC LIMIT $1 [["LIMIT", 1]] => # 2.3.1p112 :123 > p.bips Package::Bip Load (0.5ms) SELECT "package_bips".* FROM "package_bips" WHERE "package_bips"."ipable_id" = $1 AND "package_bips"."ipable_type" = $2 [["ipable_id", 15], ["ipable_type", "Proposal"]] => #<ActiveRecord::Associations::CollectionProxy [# 

这是对的。 我也可以这样做:

 o = Organisation.first Organisation Load (1.1ms) SELECT "organisations".* FROM "organisations" ORDER BY "organisations"."id" ASC LIMIT $1 [["LIMIT", 1]] => # 2.3.1p112 :125 > o.bips Package::Bip Load (0.4ms) SELECT "package_bips".* FROM "package_bips" WHERE "package_bips"."ipable_id" = $1 AND "package_bips"."ipable_type" = $2 [["ipable_id", 1], ["ipable_type", "Organisation"]] => # 

这也是正确的。

我只是找不到一种方法来在代码中使用正确的bips填充索引。

另一个尝试

我正在尝试使用本教程中的建议: http : //www.codequizzes.com/rails/learn-rails/polymorphism

它表明:

 # views/articles/show.html.erb  # views/comments/_comment.html.erb 
The render @article.comments in the show page automatically knows to load a file called views/comments/_comment.html.erb. This is Rails magic, so just memorize this.

考虑到这一点,我在我的views / package / bips文件夹中创建了一个名为_bips.html.erb的文件夹。

在我的提案显示视图中,我试过:

  

我收到一条错误消息:

 Missing partial package/bips/_bip with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :coffee, :jbuilder]}. Searched in: * "/Users/d/cv/cflive/app/views" 

那是正确的位置。 我想知道这个问题是否与嵌套路由有关。 我的路线文件现在有:

 resources :proposals do namespace :package do resources :bips end 

任何人都可以看到为什么这种方法不起作用?