我使用https://github.com/crowdint/rails3-jquery-autocomplete它似乎工作。唯一的問題是可以說我有一個字段在所有帖子標題上執行自動完成,但我希望它只顯示用戶創建的帖子的標題。範圍爲rails3的結果jQuery的自動完成插件
有沒有辦法做這樣的事情?即find_by或者什麼?
謝謝!
-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
我覺得這可能是用來做什麼,我試圖完成的發生,但我不知道從哪裏開始?
嗨Sameera,我想: 範圍:funky_method,拉姆達{ 其中( 「post.user_id樣?」,current_user.id) } 但它似乎沒有工作...? – Elliot 2011-02-01 12:13:02
不幸的是,我無法讓範圍適合這顆寶石。我不得不直接修改get_autocomplete_method。好消息是,您可以使用Rails的選擇器查詢或直接SQL來修改get_autocomplete_items方法。在這個例子中,我正在尋找名字和姓氏的匹配。 https://gist.github.com/bb3b54f2ec793cf00fd0 – slant 2011-03-14 00:23:17