My rails application is used acts-as-taggable-on. I would like to add function which articles can be searched by tags.
In my articles_controller.rb
def index
@articles = params[:tag].present? ? Article.tagged_with(params[:tag]) : Article.all
@articles = @articles.includes(:tags)
@articles = Article.page(params[:page])
end
In index.html.erb
<%= render partial:'tags',locals:{tags: article.tags}%>
In _tags.html.erb
<% tags.each do |tag| %>
<%= link_to "#{tag.name} (#{tag.taggings_count})", articles_path(tag: tag.name) %>
<%end%>
However, article wasn't scoped by tags. I confirmed that acts-as-taggable-on is working. Only tagged_with method doesn't work.
If you know any solution, please tell me.
In Active record
irb(main):001:0> Article.tagged_with("アップルパイ")
ActsAsTaggableOn::Tag Load (25.9ms) SELECT "tags".* FROM "tags" WHERE (LOWER(name) = LOWER('アップルパイ'))
Article Load (2.6ms) SELECT "articles".* FROM "articles" JOIN taggings articles_taggings_2902983 ON articles_taggings_2902983.taggable_id = articles.id AND articles_taggings_2902983.taggable_type = 'Article' AND articles_taggings_2902983.tag_id = 2
=> #<ActiveRecord::Relation [#<Article id: 6, title: "横浜のアップルパイ専門店", content: "横浜の赤レンガ倉庫にオープンしたアップルパイ専門のお店。写真のラズベリーソースは人気の味", created_at: "2017ge: "ras.jpg">, #<Article id: 18, title: "tagfie", content: "タグの機能を修理するためのテスト記事です。", created_at: "2017-04-05 15:41:23", updated_at: "2017-04-05 16:48:39", image: nil>, #<Article pie", content: "Apple pieの記事です", created_at: "2017-04-09 10:36:38", updated_at: "2017-04-09 10:36:38", image: nil>]>
irb(main):002:0>
However, by rails s
ActiveRecord::SchemaMigration Load (2.7ms) SELECT "schema_migrations".* FROM "schema_migrations"
DEPRECATION WARNING: before_filter is deprecated and will be removed in Rails 5.1. Use before_action instead. (called from <class:ApplicationController> at /Users/futamidaiki/Desktop/lablab/choco/app/controllers/application_controller.rb:2)
Processing by ArticlesController#index as HTML
Parameters: {"tag"=>"アップルパイ"}
ActsAsTaggableOn::Tag Load (5.8ms) SELECT "tags".* FROM "tags" WHERE (LOWER(name) = LOWER('アップルパイ'))
Rendering articles/index.html.erb within layouts/application
Article Load (2.7ms) SELECT "articles".* FROM "articles" LIMIT ? OFFSET ? [["LIMIT", 10], ["OFFSET", 0]]
ActsAsTaggableOn::Tag Load (3.7ms) SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = ? AND "taggings"."taggable_type" = ? AND "taggings"."context" = ? [["taggable_id", 5], ["taggable_type", "Article"], ["context", "tags"]]
Rendered articles/_tags.html.erb (7.8ms)
User Load (3.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 2], ["LIMIT", 1]]
ActsAsTaggableOn::Tag Load (0.2ms) SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = ? AND "taggings"."taggable_type" = ? AND "taggings"."context" = ? [["taggable_id", 6], ["taggable_type", "Article"], ["context", "tags"]]
Rendered articles/_tags.html.erb (2.4ms)
ActsAsTaggableOn::Tag Load (0.5ms) SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = ? AND "taggings"."taggable_type" = ? AND "taggings"."context" = ? [["taggable_id", 7], ["taggable_type", "Article"], ["context", "tags"]]
Rendered articles/_tags.html.erb (2.8ms)
ActsAsTaggableOn::Tag Load (0.3ms) SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = ? AND "taggings"."taggable_type" = ? AND "taggings"."context" = ? [["taggable_id", 8], ["taggable_type", "Article"], ["context", "tags"]]
Rendered articles/_tags.html.erb (2.3ms)
ActsAsTaggableOn::Tag Load (0.2ms) SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = ? AND "taggings"."taggable_type" = ? AND "taggings"."context" = ? [["taggable_id", 10], ["taggable_type", "Article"], ["context", "tags"]]
Rendered articles/_tags.html.erb (1.7ms)
ActsAsTaggableOn::Tag Load (0.2ms) SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = ? AND "taggings"."taggable_type" = ? AND "taggings"."context" = ? [["taggable_id", 11], ["taggable_type", "Article"], ["context", "tags"]]
Rendered articles/_tags.html.erb (1.4ms)
ActsAsTaggableOn::Tag Load (0.2ms) SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = ? AND "taggings"."taggable_type" = ? AND "taggings"."context" = ? [["taggable_id", 18], ["taggable_type", "Article"], ["context", "tags"]]
Rendered articles/_tags.html.erb (1.8ms)
ActsAsTaggableOn::Tag Load (0.2ms) SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = ? AND "taggings"."taggable_type" = ? AND "taggings"."context" = ? [["taggable_id", 19], ["taggable_type", "Article"], ["context", "tags"]]
Rendered articles/_tags.html.erb (1.7ms)
ActsAsTaggableOn::Tag Load (0.3ms) SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = ? AND "taggings"."taggable_type" = ? AND "taggings"."context" = ? [["taggable_id", 20], ["taggable_type", "Article"], ["context", "tags"]]
Rendered articles/_tags.html.erb (1.8ms)
(0.2ms) SELECT COUNT(*) FROM "articles"
Rendered articles/index.html.erb within layouts/application (1827.4ms)
ActsAsTaggableOn::Tag Load (0.5ms) SELECT "tags".* FROM "tags" WHERE (LOWER(name) = LOWER('渋谷'))
ActsAsTaggableOn::Tag Load (0.3ms) SELECT "tags".* FROM "tags" WHERE (LOWER(name) = LOWER('表参道'))
ActsAsTaggableOn::Tag Load (0.3ms) SELECT "tags".* FROM "tags" WHERE (LOWER(name) = LOWER('青山'))
ActsAsTaggableOn::Tag Load (0.2ms) SELECT "tags".* FROM "tags" WHERE (LOWER(name) = LOWER('横浜'))
ActsAsTaggableOn::Tag Load (0.3ms) SELECT "tags".* FROM "tags" WHERE (LOWER(name) = LOWER('その他'))
Completed 200 OK in 3143ms (Views: 2865.4ms | ActiveRecord: 21.7ms)
It looks like you are reassigning @articles
in your controller to use pagination, which is overwriting .includes
from the previous line. Try:
def index
articles = params[:tag].present? ? Article.tagged_with(params[:tag]) : Article.all
articles = articles.includes(:tags)
@articles = articles.page(params[:page])
end