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.
我正在使用https://github.com/crowdint/rails3-jquery-autocomplete,它似乎正在工作。唯一的问题是假设我有一个字段在所有帖子标题上执行自动完成,但我希望它只显示用户创建的帖子标题。
Is there a way to do something like this? I.e. a find_by or something?
有没有办法做这样的事情?即一个find_by还是什么?
Thanks!
-Elliot
EDIT
it says in the documentation:
它在文档中说:
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?
我觉得这可以用来做我想要完成的事情,但我不知道从哪里开始?
3 个解决方案
#1
2
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:
你应该覆盖方法get_item,例如在这些情况下我将过滤用户列表考虑到最后一个:
class UsersController < ApplicationController
autocomplete :user, :email, :full => true
def index
@users = User.all
end
def get_items(parameters)
[User.last]
end
I think that one of the last commits, changed the method (#get_items) name. Check you version of autocomplete.rb file.
我认为最后一次提交之一,改变了方法(#get_items)的名称。检查autocomplete.rb文件的版本。
Check it here: https://github.com/crowdint/rails3-jquery-autocomplete/commit/b3c18ac92ced2932ac6e9d17237343b512d2144d#L1R84.
请在此处查看:https://github.com/crowdint/rails3-jquery-autocomplete/commit/b3c18ac92ced2932ac6e9d17237343b512d2144d#L1R84。
#2
7
You can override get_autocomplete_items
, but first use super to call the original get_autocomplete_items
method, and then filter the results by current_user.id (or post.user.id if the owner of the post is not the current user)
您可以覆盖get_autocomplete_items,但首先使用super来调用原始的get_autocomplete_items方法,然后通过current_user.id过滤结果(如果帖子的所有者不是当前用户,则过滤post.user.id)
def get_autocomplete_items(parameters)
items = super(parameters)
items = items.where(:user_id => current_user.id)
end
This way you can still all the other options that come with the gem. This should be done in your controller by the way.
这样你仍然可以使用gem附带的所有其他选项。这应该在您的控制器中完成。
Another answer suggested the :scope
option. I looked into that, but it is not the way to go in this case, you don't want to add "current_user" and things like that to your models.
另一个答案建议:范围选项。我调查了一下,但在这种情况下不是这样的,你不想在模型中添加“current_user”等类似的东西。
#3
1
Even though I havent personally tested, I think normal scope method in rails3 should work. As after all scope is also a method same as "funky_method"
即使我没有亲自测试过,我认为rails3中的正常范围方法应该可行。因为所有范围也是与“funky_method”相同的方法
write a scope
写一个范围
in your model
在你的模型中
scope :funky_method, lambda { |param1| :conditions => {:column = param1} }
范围:funky_method,lambda {| param1 | :conditions => {:column = param1}}
and in your controller
并在你的控制器
autocomplete :brand, :name, :display_value => :funky_method
autocomplete:brand,:name,:display_value =>:funky_method
should work, try and see..
应该工作,试着看看..
cheers
sameera
#1
2
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:
你应该覆盖方法get_item,例如在这些情况下我将过滤用户列表考虑到最后一个:
class UsersController < ApplicationController
autocomplete :user, :email, :full => true
def index
@users = User.all
end
def get_items(parameters)
[User.last]
end
I think that one of the last commits, changed the method (#get_items) name. Check you version of autocomplete.rb file.
我认为最后一次提交之一,改变了方法(#get_items)的名称。检查autocomplete.rb文件的版本。
Check it here: https://github.com/crowdint/rails3-jquery-autocomplete/commit/b3c18ac92ced2932ac6e9d17237343b512d2144d#L1R84.
请在此处查看:https://github.com/crowdint/rails3-jquery-autocomplete/commit/b3c18ac92ced2932ac6e9d17237343b512d2144d#L1R84。
#2
7
You can override get_autocomplete_items
, but first use super to call the original get_autocomplete_items
method, and then filter the results by current_user.id (or post.user.id if the owner of the post is not the current user)
您可以覆盖get_autocomplete_items,但首先使用super来调用原始的get_autocomplete_items方法,然后通过current_user.id过滤结果(如果帖子的所有者不是当前用户,则过滤post.user.id)
def get_autocomplete_items(parameters)
items = super(parameters)
items = items.where(:user_id => current_user.id)
end
This way you can still all the other options that come with the gem. This should be done in your controller by the way.
这样你仍然可以使用gem附带的所有其他选项。这应该在您的控制器中完成。
Another answer suggested the :scope
option. I looked into that, but it is not the way to go in this case, you don't want to add "current_user" and things like that to your models.
另一个答案建议:范围选项。我调查了一下,但在这种情况下不是这样的,你不想在模型中添加“current_user”等类似的东西。
#3
1
Even though I havent personally tested, I think normal scope method in rails3 should work. As after all scope is also a method same as "funky_method"
即使我没有亲自测试过,我认为rails3中的正常范围方法应该可行。因为所有范围也是与“funky_method”相同的方法
write a scope
写一个范围
in your model
在你的模型中
scope :funky_method, lambda { |param1| :conditions => {:column = param1} }
范围:funky_method,lambda {| param1 | :conditions => {:column = param1}}
and in your controller
并在你的控制器
autocomplete :brand, :name, :display_value => :funky_method
autocomplete:brand,:name,:display_value =>:funky_method
should work, try and see..
应该工作,试着看看..
cheers
sameera