Javascript 确定rails3 jquery自动完成插件的结果范围

Javascript 确定rails3 jquery自动完成插件的结果范围,javascript,jquery,ruby-on-rails,ruby,ruby-on-rails-3,Javascript,Jquery,Ruby On Rails,Ruby,Ruby On Rails 3,我正在使用,它似乎在工作。唯一的问题是,假设我有一个字段对所有帖子标题执行自动完成,但我希望它只显示用户创建的帖子标题 有没有办法做到这一点?也就是说,找你什么的 谢谢 -艾略特 编辑 文档中写道: 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

我正在使用,它似乎在工作。唯一的问题是,假设我有一个字段对所有帖子标题执行自动完成,但我希望它只显示用户创建的帖子标题

有没有办法做到这一点?也就是说,找你什么的

谢谢

-艾略特

编辑

文档中写道:

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
如果要显示所需内容的不同版本,可以使用:display_value选项。
此选项接收一个方法名作为参数,并且在显示结果时将在实例上调用该方法。
类别品牌:funky\u方法
结束
在上面的示例中,您将按名称搜索,但自动完成列表将显示funky_方法的结果

我觉得这可以用来实现我想要实现的目标,但我不知道从哪里开始?

尽管我还没有亲自测试过,但我认为rails3中的正常范围方法应该可以工作。毕竟,scope也是一种与“funky_方法”相同的方法

写一个范围

在您的模型中

范围:funky|u方法,lambda{124; param1| :条件=>{:column=param1} }

在你的控制器里

自动完成:brand,:name,:display\u value=>:funky\u方法

你应该工作,试试看

干杯


“SeaErrase/P>>P>您应该改写该方法<强> GETX项,例如,在这些情况下,我正在过滤用户列表,考虑到最后一个:

class UsersController < ApplicationController
    autocomplete :user, :email, :full => true

    def index
        @users = User.all
    end

    def get_items(parameters)
        [User.last]
    end
class UsersControllertrue
def索引
@users=User.all
结束
def get_项目(参数)
[用户.最后]
结束
我认为最后的一次提交更改了方法(#get_items)的名称。检查autocomplete.rb文件的版本


请在此处查看:

您可以覆盖
get\u autocomplete\u items
,但首先使用super调用原始的
get\u autocomplete\u items
方法,然后通过当前用户.id过滤结果(如果帖子的所有者不是当前用户,则使用post.user.id)

这样,您仍然可以使用gem附带的所有其他选项。顺便说一下,这应该在您的控制器中完成


另一个答案建议使用
:scope
选项。我对此进行了研究,但在这种情况下,这不是一种方法,你不想在你的模型中添加“当前用户”之类的东西

嗨,Sameera,我试过:scope:funky_方法,lambda{where(“post.user_id LIKE?”,current_user.id)}但它似乎不起作用…?很遗憾,我无法让scope为这个gem工作。我不得不直接修改get\u autocomplete\u方法。好消息是,您可以使用Rails的选择器查询或直接SQL修改get_autocomplete_items方法。在这个例子中,我正在寻找名字和姓氏的匹配。嗨,ClaudioA,这在一定程度上是有效的:def get_items(parameters)Post。其中(:user_id=>current_user.id)结束,但自动完成不再起作用,它只显示满足这些需求的项的列表。您仍然可以通过调用super使用原始get_items方法,更多关于答案的详细信息
 def get_autocomplete_items(parameters)
    items = super(parameters)
    items = items.where(:user_id => current_user.id)
  end