在 Rails 中使用 acts_as_taggable_on 搜索表单 4
Search form with acts_as_taggable_on in Rails 4
我正在创建一个 rails 应用程序,允许用户创建 post 并向其添加标签。我正在尝试实现一个搜索栏,它可以搜索标签并通过显示包含用户搜索的标签的所有 post 来过滤结果。我已经安装了 acts_as_taggable_on gem.
这是我的 Post 模型,我在其中尝试实施目前不起作用的搜索方法:
class Post < ActiveRecord::Base
belongs_to :user
acts_as_taggable
def self.search(search)
where("tag_list LIKE", "%#{search}")
end
end
这是我的 Post 带有索引方法和私有方法的控制器:
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy, :upvote]
before_filter :authenticate_user!, except: [:index, :show]
def index
if params[:search]
@posts = Post.search(params[:search]).order("created_at DESC")
else
@posts = Post.all
end
end
private
def set_post
@post = Post.find(params[:id])
end
def post_params
params.require(:post).permit(:title, :content, :tag_list, :user_id)
end
end
这就是我在我的观点中实现搜索表单的方式:
<%= form_tag(posts_path, :method => 'get', id: "search-form") do %>
<%= text_field_tag :search,params[:search], placeholder: "Search Tags" %>
<%= submit_tag "Search" %>
<% end %>
迁移(全部由 acts_as_taggable_on gem 创建):
class CreatePosts < ActiveRecord::Migration
def change
create_table :posts do |t|
t.string :title
t.text :content
t.timestamps null: false
end
end
end
class ActsAsTaggableOnMigration < ActiveRecord::Migration
def self.up
create_table :tags do |t|
t.string :name
end
create_table :taggings do |t|
t.references :tag
t.references :taggable, polymorphic: true
t.references :tagger, polymorphic: true
t.string :context, limit: 128
t.datetime :created_at
end
add_index :taggings, :tag_id
add_index :taggings, [:taggable_id, :taggable_type, :context]
end
def self.down
drop_table :taggings
drop_table :tags
end
end
class AddMissingUniqueIndices < ActiveRecord::Migration
def self.up
add_index :tags, :name, unique: true
remove_index :taggings, :tag_id
remove_index :taggings, [:taggable_id, :taggable_type, :context]
add_index :taggings, [:tag_id, :taggable_id, :taggable_type, :context, :tagger_id, :tagger_type], unique: true, name: 'taggings_idx'
end
def self.down
remove_index :tags, :name
remove_index :taggings, name: 'taggings_idx'
add_index :taggings, :tag_id
add_index :taggings, [:taggable_id, :taggable_type, :context]
end
end
class AddTaggingsCounterCacheToTags < ActiveRecord::Migration
def self.up
add_column :tags, :taggings_count, :integer, default: 0
ActsAsTaggableOn::Tag.reset_column_information
ActsAsTaggableOn::Tag.find_each do |tag|
ActsAsTaggableOn::Tag.reset_counters(tag.id, :taggings)
end
end
def self.down
remove_column :tags, :taggings_count
end
end
class AddMissingTaggableIndex < ActiveRecord::Migration
def self.up
add_index :taggings, [:taggable_id, :taggable_type, :context]
end
def self.down
remove_index :taggings, [:taggable_id, :taggable_type, :context]
end
end
class ChangeCollationForTagNames < ActiveRecord::Migration
def up
if ActsAsTaggableOn::Utils.using_mysql?
execute("ALTER TABLE tags MODIFY name varchar(255) CHARACTER SET utf8 COLLATE utf8_bin;")
end
end
end
您不需要编写自己的范围 - Acts as Taggable 附带一个适合您的情况:tagged_with
举个例子:https://github.com/mbleigh/acts-as-taggable-on#finding-tagged-objects
删除您的 search
范围并更改您的 controller#index 方法来执行此操作:
@posts = Post.tagged_with(params[:search]).order("created_at DESC")
我正在创建一个 rails 应用程序,允许用户创建 post 并向其添加标签。我正在尝试实现一个搜索栏,它可以搜索标签并通过显示包含用户搜索的标签的所有 post 来过滤结果。我已经安装了 acts_as_taggable_on gem.
这是我的 Post 模型,我在其中尝试实施目前不起作用的搜索方法:
class Post < ActiveRecord::Base
belongs_to :user
acts_as_taggable
def self.search(search)
where("tag_list LIKE", "%#{search}")
end
end
这是我的 Post 带有索引方法和私有方法的控制器:
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy, :upvote]
before_filter :authenticate_user!, except: [:index, :show]
def index
if params[:search]
@posts = Post.search(params[:search]).order("created_at DESC")
else
@posts = Post.all
end
end
private
def set_post
@post = Post.find(params[:id])
end
def post_params
params.require(:post).permit(:title, :content, :tag_list, :user_id)
end
end
这就是我在我的观点中实现搜索表单的方式:
<%= form_tag(posts_path, :method => 'get', id: "search-form") do %>
<%= text_field_tag :search,params[:search], placeholder: "Search Tags" %>
<%= submit_tag "Search" %>
<% end %>
迁移(全部由 acts_as_taggable_on gem 创建):
class CreatePosts < ActiveRecord::Migration
def change
create_table :posts do |t|
t.string :title
t.text :content
t.timestamps null: false
end
end
end
class ActsAsTaggableOnMigration < ActiveRecord::Migration
def self.up
create_table :tags do |t|
t.string :name
end
create_table :taggings do |t|
t.references :tag
t.references :taggable, polymorphic: true
t.references :tagger, polymorphic: true
t.string :context, limit: 128
t.datetime :created_at
end
add_index :taggings, :tag_id
add_index :taggings, [:taggable_id, :taggable_type, :context]
end
def self.down
drop_table :taggings
drop_table :tags
end
end
class AddMissingUniqueIndices < ActiveRecord::Migration
def self.up
add_index :tags, :name, unique: true
remove_index :taggings, :tag_id
remove_index :taggings, [:taggable_id, :taggable_type, :context]
add_index :taggings, [:tag_id, :taggable_id, :taggable_type, :context, :tagger_id, :tagger_type], unique: true, name: 'taggings_idx'
end
def self.down
remove_index :tags, :name
remove_index :taggings, name: 'taggings_idx'
add_index :taggings, :tag_id
add_index :taggings, [:taggable_id, :taggable_type, :context]
end
end
class AddTaggingsCounterCacheToTags < ActiveRecord::Migration
def self.up
add_column :tags, :taggings_count, :integer, default: 0
ActsAsTaggableOn::Tag.reset_column_information
ActsAsTaggableOn::Tag.find_each do |tag|
ActsAsTaggableOn::Tag.reset_counters(tag.id, :taggings)
end
end
def self.down
remove_column :tags, :taggings_count
end
end
class AddMissingTaggableIndex < ActiveRecord::Migration
def self.up
add_index :taggings, [:taggable_id, :taggable_type, :context]
end
def self.down
remove_index :taggings, [:taggable_id, :taggable_type, :context]
end
end
class ChangeCollationForTagNames < ActiveRecord::Migration
def up
if ActsAsTaggableOn::Utils.using_mysql?
execute("ALTER TABLE tags MODIFY name varchar(255) CHARACTER SET utf8 COLLATE utf8_bin;")
end
end
end
您不需要编写自己的范围 - Acts as Taggable 附带一个适合您的情况:tagged_with
举个例子:https://github.com/mbleigh/acts-as-taggable-on#finding-tagged-objects
删除您的 search
范围并更改您的 controller#index 方法来执行此操作:
@posts = Post.tagged_with(params[:search]).order("created_at DESC")