I am on Rails 4 using Acts-As-Taggable-On
On my Articles show page I am building a "Related Articles" section according to tags.
I have gotten it to work but it includes the current article as well. Having trouble figuring out how to show all related articles but exclude the article it's already showing.
Here is what I am using in my articles_controller:
@related_articles = Article.tagged_with(@article.tag_list, any: true)
and here is my view:
<% @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 %>
What is the best way to go about excluding the current article?
I ended up using the reject
method in order to exclude the article if it was the one being displayed.
I used this in the Show action of Articles controller:
@related_articles = Article.tagged_with(@article.tag_list, any: true).page(params[:page]).per(4)
@other_articles = @related_articles.reject {|article| article == @article}
And then iterate over the other articles:
<% @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 %>
Works like a charm.