所以我使用this寶石自動完成。如用法部分所述,它要求我們在模型的控制器中添加一條autocomplete :classname, :method
行,該模型的屬性是我們嘗試自動完成的。因此,例如(來自文檔中提取):使用acts_as_taggable_on gem時自動完成標籤名稱?
模型
class Brand < ActiveRecord::Base
end
create_table :brand do |t|
t.column :name, :string
end
控制器
class ProductsController < Admin::BaseController
autocomplete :brand, :name
end
路線
resources :products do
get :autocomplete_brand_name, :on => :collection
end
我爲我的代碼使用acts_as_taggable_on寶石。每個標籤都有一個name
屬性,我試圖自動完成。現在,我不確定在TagsController的autocomplete :classname, :method
行中使用什麼類名稱。類的名稱是ActsAsTaggableOn ::標籤,但是當我用我得到一個錯誤:
TagsController
class TagsController < ApplicationController
autocomplete :ActsAsTaggableOn::Tag, :name
def index
@tags = ActsAsTaggableOn::Tag.all
end
def show
@tag = ActsAsTaggableOn::Tag.find_by(name: params[:name])
@posts = Post.tagged_with(@tag.name).paginate(page: params[:page], per_page: 10).order('created_at DESC')
end
end
路線
resources :tags, param: :name, only: [:index, :show] do
get :autocomplete_ActsAsTaggableOn::Tag_name, :on => :collection
end
錯誤後rake routes
TypeError: :ActsAsTaggableOn is not a class/module
我試過tag
和tags
作爲類名。但是,他們沒有返回錯誤,但他們根本沒有工作。任何幫助表示讚賞!
您可以發佈您控制器的確切的代碼? ':ActsAsTaggableOn'中的列看起來很奇怪.. –
@MarekTakac我用TagsController更新了這篇文章。 ProductsController實際上只是一個示例控制器,從我提供的gem文檔中提取出來,以顯示'autocomplete'行應該去哪裏 – Arif