我的rails應用程序使用acts-as-taggable-on。我想添加哪些文章可以通過標籤搜索的功能。tagged_with方法不起作用
在我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%>
然而,文章不是由作用域標籤。我確認行爲作爲標籤在工作。只有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>
然而,軌道小號
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)
順便說一句,這是我的視圖文件
index.html.erb
<% @search_articles.each_with_index do|article,i|%>
<% if i==0 %>
<ul class="article" style="list-style:none;">
<li>
<% if article.image?%>
<%= image_tag article.image.url %>
<%else%>
<%= image_tag 'noimage.png'%>
<%end%>
<%= link_to article.title, article_path(article.id) %>
<div class="description">
<%= article.content%>
</div>
<%= render partial:'tags',locals:{tags: article.tags}%>
<%if user_signed_in?%>
<%= link_to '編集',edit_article_path(article.id) %>
<%end%>
<div class="firstborder">
</div>
</li>
</ul>
<%else%>
<ul class="second" style="list-style:none;">
<li> <% if article.image?%>
<%= image_tag article.image.url %>
<%else%>
<%= image_tag 'noimage.png'%>
<%end%>
<%= link_to article.title, article_path(article.id) %>
<%= render partial:'tags',locals:{tags: article.tags}%>
<%if user_signed_in?%>
<%= link_to '編集',edit_article_path(article.id) %>
<%end%>
<div class="secondborder">
</div>
</li>
</ul>
<%end%>
<%end%>
<% if user_signed_in?%>
<%= link_to '記事を投稿する',new_article_path %>
<%else%>
<%= link_to '管理者としてログイン',new_user_session_path,{:class => 'log'} %>
<%end%>
<div class="paginate">
<%= paginate @articles%>
</div>
application.html.erb
<html>
<head>
<title>Sweets Maphia</title>
<%= csrf_meta_tags %>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= include_gon %>
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
</head>
<dl class="searchbox">
<%= search_form_for @search, url: articles_path do |f| %>
<dt><%= f.text_field :title_or_content_cont ,placeholder: '検索するテキストを入力...' %></dt>
<dd><%= f.submit ('検索') , class: 'search-btn'%></dd>
<% end %>
</dl>
<%= link_to image_tag('swm.png'),articles_path,:class => 'topof' %>
<div class="menu">
<div class="shibuya">
<%= link_to 'SHIBUYA', tags: Article.tagged_with('渋谷')%>
<%= link_to 'OMOTE-SANDOH',tags: Article.tagged_with('表參道')%>
<%= link_to 'AOYAMA',tags: Article.tagged_with('青山')%>
<%= link_to 'YOKOHAMA',tags: Article.tagged_with('橫浜')%>
<%= link_to 'OTHERS',tags: Article.tagged_with('その他')%>
</div>
</div>
<div class="border">
</div>
<body>
<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>
<%= yield %>
</body>
<footer>
<p><span>Sweets Maphia</span>
Sweets Maphia, inc. All Rights Reserved 複製及び無斷転載禁止</p>
</footer>
</html>
導軌5.0.2,行爲-AS-加標籤上=> 4.0,紅寶石2.3.0 – Justin
檢查你的開發日誌中SQL由此查詢生成並運行並在此處共享。 – coreyward
@coreyward我添加了活動記錄日誌。 – Justin