我的 rails 控制器不工作
My rails controller didn't work
我的 rails 应用程序被用作 acts-as-taggable-on。我想添加可以通过标签搜索文章的功能。
在我的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
在index.html.erb
<%= render partial:'tags',locals:{tags: article.tags}%>
在_tags.html.erb
<% tags.each do |tag| %>
<%= link_to "#{tag.name} (#{tag.taggings_count})", articles_path(tag: tag.name) %>
<%end%>
但是,文章不受标签限制。我确认 acts-as-taggable-on 正在工作。只有 tagged_with 方法不起作用。
如果你知道任何解决方案,请告诉我。
在活动记录中
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>
然而,通过 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)
您似乎正在控制器中重新分配 @articles
以使用分页,这会覆盖上一行的 .includes
。尝试:
def index
articles = params[:tag].present? ? Article.tagged_with(params[:tag]) : Article.all
articles = articles.includes(:tags)
@articles = articles.page(params[:page])
end
我的 rails 应用程序被用作 acts-as-taggable-on。我想添加可以通过标签搜索文章的功能。
在我的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
在index.html.erb
<%= render partial:'tags',locals:{tags: article.tags}%>
在_tags.html.erb
<% tags.each do |tag| %>
<%= link_to "#{tag.name} (#{tag.taggings_count})", articles_path(tag: tag.name) %>
<%end%>
但是,文章不受标签限制。我确认 acts-as-taggable-on 正在工作。只有 tagged_with 方法不起作用。
如果你知道任何解决方案,请告诉我。
在活动记录中
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>
然而,通过 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)
您似乎正在控制器中重新分配 @articles
以使用分页,这会覆盖上一行的 .includes
。尝试:
def index
articles = params[:tag].present? ? Article.tagged_with(params[:tag]) : Article.all
articles = articles.includes(:tags)
@articles = articles.page(params[:page])
end