Rails多态link_to来自儿童索引页面的父级节目页面

时间:2017-10-08 00:04:45

标签: ruby-on-rails polymorphic-associations

我有三个模特

class Comment < ActiveRecord::Base  
  belongs_to :commentable, :polymorphic => true
end

class Movie < ActiveRecord::Base
  has_many :comments, :as => :commentable
end

class TV < ActiveRecord::Base
  has_many :comment, :as => :commentable
end

在我的主页中,人们可以看到评论的索引页面等热门评论。

我目前的行为如下,但它不起作用

<div>
  <% @comments.each do |comment| %>
    <%= simple_format comment.content %>
    <%= link_to comment.commentable_id %>
  <% end %>
</div>

似乎我错过了一些东西。我应该添加什么才能通过评论链接到家长的节目页面?

也就是说,例如,有人发表评论&#34;这部电影很精彩&#34;在一部电影,变形金刚。

人们也可以在评论的索引页面上看到评论。然后有一个链接将它们带到Transformer的显示页面,以便用户能够看到更多细节。

我是否还需要在控制器上做任何事情?

1 个答案:

答案 0 :(得分:0)

搞清楚 只需编辑如下

<div>
  <% @comments.each do |comment| %>
    <%= simple_format comment.content %>
    <%= link_to **"LINK", comment.commentable** %>
  <% end %>
</div>