确定rails3 jquery autocomplete插件的结果范围 [英] Scoping the results for rails3 jquery autocomplete plugin
问题描述
我正在使用 https://github.com/crowdint/rails3-jquery-autocomplete 它似乎有效。唯一的问题是让我说我有一个字段在所有帖子标题上执行自动完成,但我希望它只显示用户创建的帖子的标题。
I'm using https://github.com/crowdint/rails3-jquery-autocomplete and it seems to be working. The only problem is lets say I have a field performing an autocomplete on all post titles, but I want it to only show titles of posts the user has created.
有没有办法做这样的事情?即一个find_by或者什么?
Is there a way to do something like this? I.e. a find_by or something?
谢谢!
-Elliot
编辑
它在文档中说明:
If you want to display a different version of what you're looking for, you can use the :display_value option.
This options receives a method name as the parameter, and that method will be called on the instance when displaying the results.
class Brand < ActiveRecord::Base
def funky_method
"#{self.name}.camelize"
end
end
class ProductsController < Admin::BaseController
autocomplete :brand, :name, :display_value => :funky_method
end
In the example above, you will search by name, but the autocomplete list will display the result of funky_method
我觉得这可以用来做我想要完成的事情,但我不知道从哪里开始?
I feel like this could be used to make what I'm trying to accomplish happen, but I have no idea where to begin?
推荐答案
你应该覆盖方法 get_item ,例如在这些情况下我将过滤用户列表考虑到最后一个:
You should overwrite the method get_item, for example in these case I'm filtering the list of users to consider to just the last one:
class UsersController < ApplicationController
autocomplete :user, :email, :full => true
def index
@users = User.all
end
def get_items(parameters)
[User.last]
end
我认为最后一次提交之一,改变了方法(#get_items)的名称。检查 autocomplete.rb 文件的版本。
I think that one of the last commits, changed the method (#get_items) name. Check you version of autocomplete.rb file.
这篇关于确定rails3 jquery autocomplete插件的结果范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!