Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在视图中分离两种多态关联_Ruby On Rails_Ruby On Rails 3_Polymorphic Associations - Fatal编程技术网

Ruby on rails 在视图中分离两种多态关联

Ruby on rails 在视图中分离两种多态关联,ruby-on-rails,ruby-on-rails-3,polymorphic-associations,Ruby On Rails,Ruby On Rails 3,Polymorphic Associations,我有属于标签的团队和玩家之间的多态关联,每个标签都属于一篇文章 class Tag < ActiveRecord::Base attr_accessible :article_id, :tagable_id, :tagable_type belongs_to :tagable, :polymorphic => true belongs_to :article end class标记true 属于:物品 结束 既然两支球队和球员都有不同的领域,我怎么能在我的文章展示页

我有属于标签的团队和玩家之间的多态关联,每个标签都属于一篇文章

class Tag < ActiveRecord::Base
  attr_accessible :article_id, :tagable_id, :tagable_type

  belongs_to :tagable, :polymorphic => true
  belongs_to :article
end
class标记true
属于:物品
结束
既然两支球队和球员都有不同的领域,我怎么能在我的文章展示页面中将两者分开呢

这不管用

  <% @article.tags.each do |tag| %>
      <%= tag.nickname if tag.tagable_type = "Player" %>
      <%= tag.name if tag.tagable_type = "Team" %>
  <% end %>

试试这个:

<% @article.tags.each do |tag| %>
  <%= tag.nickname if tag.tagable_type == "Player" %>
  <%= tag.name if tag.tagable_type == "Team" %>
<% end %>

Thank解决了这个问题,必须使用tag.tagable.nickname而不是tag.nickname。