【问题标题】:Search form with acts_as_taggable_on in Rails 4在 Rails 4 中使用acts_as_taggable_on 搜索表单
【发布时间】:2016-01-24 14:37:23
【问题描述】:

我正在创建一个允许用户创建帖子并向其添加标签的 Rails 应用程序。我正在尝试实现一个搜索栏,它可以搜索标签并通过显示包含用户搜索过的标签的所有帖子来过滤结果。我已经安装了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

【问题讨论】:

  • 您能否将服务器日志中生成的参数和 ActiveRecord 调用发布到问题中?

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


【解决方案1】:

您不需要编写自己的范围 - 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")

【讨论】:

    猜你喜欢
    • 2011-07-02
    • 1970-01-01
    • 1970-01-01
    • 2015-07-16
    • 1970-01-01
    • 1970-01-01
    • 2014-07-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多