从Rails3中-jquery的,自动完成自定义列表 [英] Customize list from rails3-jquery-autocomplete
问题描述
我有一个酒店
模型和它的属性是:
I have a hotel
model and its attributes are:
ID,HOTEL_NAME,地址,可搜索。
当我设置搜索假
为特定的酒店,那么这家酒店不应该进来的下拉列表,当我在搜索字段中键入
when I set searchable false
for particular hotel then this hotel should not come in dropdown when i type in search field.
控制器是
class HotelsController < Admin::BaseController
autocomplete :hotel, :hotel_name, :full => true
end
如何优化这个
推荐答案
在控制器
def get_autocomplete_items(parameters)
items = active_record_get_autocomplete_items(parameters)
items = items.where(searchable: true)
end
在 active_record_get_autocomplete_items
上面的例子中,我们称之为而非超级
;有否超级因为这些方法都是动态调用由于设计来处理多个奥姆斯。在上面的例子中,假设的ActiveRecord
,但 Mongoid
和 MongoMapper
也都双双当前支持(如其他可能在将来)。上面的例子仍然可以工作,但根据您所使用的调用 active_record_get_autocomplete_items(参数)的ORM
需要改变,以配合您的驱动程序,例如,它会成为 mongo_mapper_get_autocomplete_items(参数)
为 MongoMapper
驱动程序。
In the example above we call active_record_get_autocomplete_items
rather than super
; there is no super as these methods are called dynamically due to the design to handle multiple ORMs. In the above example ActiveRecord
is assumed, however Mongoid
and MongoMapper
are also both currently supported (as others may be in the future). The example above will still work, but depending on the ORM you are using the call to active_record_get_autocomplete_items(parameters)
would need to change to match your driver, for example it would become mongo_mapper_get_autocomplete_items(parameters)
for the MongoMapper
driver.
使用范围
autocomplete :hotel, :hotel_name, :full => true, :scopes => [:searchable_allow]
在模型
scope :searchable_allow, where(searchable: true)
这篇关于从Rails3中-jquery的,自动完成自定义列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!