2016-01-24 22 views
0

我正在創建一個rails應用程序,它允許用戶創建帖子併爲其添加標籤。我試圖實現一個搜索欄,可以搜索標籤並通過顯示包含用戶搜索的標籤的所有帖子來篩選結果。我已經安裝了acts_as_taggable_on gem。在rails 4中使用acts_as_taggable_on搜索表單

這裏就是我想實現一個沒有做什麼工作的搜索方法我Post模型:

class Post < ActiveRecord::Base 
belongs_to :user 
acts_as_taggable 

def self.search(search) 
    where("tag_list LIKE", "%#{search}") 
end 
end 

這裏是我的帖子控制器指數方法和私有方法:

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 %> 

Migrati附件(由acts_as_taggable_on寶石全部創建):

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 
+0

你能後的PARAMS和交流在服務器日誌中生成的tiveRecord調用問題? – Pavan

回答

0

你不需要編寫自己的範圍 - 充當加標籤帶有一個只是您的情況:tagged_with

下面是一個例子:https://github.com/mbleigh/acts-as-taggable-on#finding-tagged-objects

刪除您search範圍和改變你的控制器#指數法做到這一點,而不是:

@posts = Post.tagged_with(params[:search]).order("created_at DESC") 
+0

美麗!這工作很好。非常感謝你 –