【问题标题】:Listing all tags for an acts_as_taggable列出acts_as_taggable的所有标签
【发布时间】:2014-04-25 16:00:35
【问题描述】:

我有一个应用程序,其中包含使用acts_as_taggable gem 的标签。我目前进行了设置,以便在事实类的索引页面上,单击任何标签按该标签过滤事实类。我现在要做的是创建一个索引页面,列出应用程序中的所有标签。这似乎相当简单......

  1. 创建一个tag.rb
  2. 创建一个tags_controller.rb
  3. tags/index.html.erb中添加一个视图

问题是这会导致我之前实现的搜索中断。如果还有其他需要的东西,请告诉我。

FactoidsController(它的索引部分)

class FactoidsController < ApplicationController
  helper_method :sort_column, :sort_direction
  before_filter :authenticate_user!

  # GET /factoids
  # GET /factoids.json
  def index
    if params[:tag]
      @factoids = Factoid.tagged_with(params[:tag]).order(sort_column + ' ' + sort_direction).paginate(:per_page => 15, :page => params[:page])
    else
      @factoids = Factoid.search(params[:search]).order(sort_column + ' ' + sort_direction).paginate(:per_page => 15, :page => params[:page])
    end

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @factoids }
    end
  end

  def tagged
    if params[:tag].present?
      @factoids = Factoid.tagged_with(params[:tag])
    else
      @factoids = Factoid.postall
    end
  end

  private
  def sort_column
    params[:sort] || "created_at"
  end

  def sort_direction
    params[:direction] || "desc"
  end

end

标签控制器

class TagsController < ApplicationController
  helper_method :sort_column, :sort_direction
  before_filter :authenticate_user!

  # GET /factoids
  # GET /factoids.json
  def index
    @tags = Tag.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @tags }
    end
  end

  private
  def sort_column
    params[:sort] || "created_at"
  end

  def sort_direction
    params[:direction] || "desc"
  end

end

路线

QaApp::Application.routes.draw do
  devise_for :users

  resources :factoids

  resources :tags

  get "home/index"

  match 'tagged' => 'factoids#tagged', :as => 'tagged'

  get 'tags/:tag', to: 'factoids#index', as: :tag

  root :to => 'home#index'

end

【问题讨论】:

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


    【解决方案1】:

    您不需要经历创建标签模型和控制器的所有麻烦。 `acts-as-taggable-on 提供了一种查找模型所有标签列表的方法。

    2.0.0-p451 :008 > Factoid.tag_counts
      ActsAsTaggableOn::Tag Load (2.0ms)  SELECT tags.*, taggings.tags_count AS count FROM "tags" JOIN (SELECT taggings.tag_id, COUNT(taggings.tag_id) AS tags_count FROM "taggings" INNER JOIN factoids ON factoids.id = taggings.taggable_id WHERE (taggings.taggable_type = 'Factoid' AND taggings.context = 'tags') AND (taggings.taggable_id IN(SELECT factoids.id FROM "factoids")) GROUP BY taggings.tag_id HAVING COUNT(taggings.tag_id) > 0) AS taggings ON taggings.tag_id = tags.id
     => #<ActiveRecord::Relation [#<ActsAsTaggableOn::Tag id: 1, name: "tag">]>
    

    这将返回所有标签对象的 ActiveRecord::Relation。您可以在其上运行 map 以获取标签数组

    Factoid.tag_counts.map(&:name)
     => ["tag"]
    

    【讨论】:

    • 这为我节省了大量工作。 :-) 非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-21
    相关资源
    最近更新 更多