【问题标题】:Acts_As_Taggable order by tag list exclude current articleActs_As_Taggable 按标签列表排序,排除当前文章
【发布时间】:2015-06-04 23:27:22
【问题描述】:

我在 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 %>

排除当前文章的最佳方法是什么?

【问题讨论】:

    标签: ruby ruby-on-rails-4 tags resources acts-as-taggable-on


    【解决方案1】:

    我最终使用了reject 方法,以便排除正在显示的文章。

    我在 Articles 控制器的 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 %>
    

    像魅力一样工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多