【发布时间】:2014-04-25 16:00:35
【问题描述】:
我有一个应用程序,其中包含使用acts_as_taggable gem 的标签。我目前进行了设置,以便在事实类的索引页面上,单击任何标签按该标签过滤事实类。我现在要做的是创建一个索引页面,列出应用程序中的所有标签。这似乎相当简单......
- 创建一个
tag.rb - 创建一个
tags_controller.rb - 在
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