tagged_with方法不起作用

时间:2017-04-14 17:15:39

标签: ruby-on-rails ruby acts-as-taggable-on tag-it

我的rails应用程序使用act-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%>

但是,文章没有标记范围。我确认act-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)

顺便说一下,这是我的视图文件

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>

1 个答案:

答案 0 :(得分: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])