我在Rails 4上使用Acts-As-Taggable-On
在我的文章显示页面上,我正在根据标签构建一个“相关文章”部分。
我已经让它开始工作,但它也包括当前的文章。很难弄清楚如何显示所有相关的文章,但不包括它已经展示的文章。
下面是我在我的articles_controller中使用的内容:
@related_articles = Article.tagged_with(@article.tag_list, any: true)以下是我的观点:
<% @related_articles.each do |article| %>
<%= link_to (image_tag article.image1(:small)), article %>
<h4 style="left"><%= link_to (article.specific.titleize), article, style:"color:#585858" %> </h4>
<% end %>排除当前文章的最佳方法是什么?
发布于 2015-06-05 02:02:47
最后,我使用了reject方法,以便排除正在显示的文章。
我在文章控制器的Show中使用了此方法:
@related_articles = Article.tagged_with(@article.tag_list, any: true).page(params[:page]).per(4)
@other_articles = @related_articles.reject {|article| article == @article}然后遍历其他文章:
<% @other_articles.each_with_index do |article, i| %>
<%= link_to (image_tag article.image1(:small)), article %>
<h4 style="left"><%= link_to (article.specific.titleize), article, style:"color:#585858" %> </h4>
<% end %>就像一种魅力。
https://stackoverflow.com/questions/30655879
复制相似问题