2017-04-14 26 views
0

我的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> 
+0

导轨5.0.2,行为-AS-加标签上=> 4.0,红宝石2.3.0 – Justin

+0

检查你的开发日志中SQL由此查询生成并运行并在此处共享。 – coreyward

+0

@coreyward我添加了活动记录日志。 – Justin

回答

1

您覆盖您@articles而失去标记查询

@articles = params[:tag].present? ? Article.tagged_with(params[:tag]) : Article.all 
    @articles = @articles.includes(:tags) 
    @articles = Article.page(params[:page]) # here, all previous results are lost 

    # did you mean this instead? 
    @articles = @articles.page(params[:page]) 
+0

谢谢你的回复。 (at)articles = Article.page(params [:page])用于分页。我重写了(at)articles =(at)articles.page(...)。不过,rails应用程序表示语法错误。 – Justin

+0

@Justin:你使用字面上的三个点吗?这应该是一个占位符。 –

+0

感谢您的回复。我重写了(at)articles =(at)articles.page(params [:page])。但是,tagged_with不起作用。当我点击任何标签时,所有文章都存在。 – Justin