回答

1

我在我的項目https://github.com/marciomr/Terra-Livre有同樣的問題,我解決它執行以下操作:

  1. 我安裝Rails3中,jQuery的自動完成功能,如供應商/ plugin目錄插件
  2. 我改變了文件助手.RB是這樣的:

def json_for_autocomplete(items, method, extra_data) 
    json = items.collect do |item| # here I put the result in a variable 
    hash = {"label" => item.send(method), "value" => item.send(method)} #here I removed the id 
    extra_data.each do |datum| 
     hash[datum] = item.send(datum) 
    end if extra_data 
    hash 
    end 
    json.uniq # this line is new 
end 

我刪除從JSON文件的ID,然後檢索uniq的值。 因爲我不需要這個ID,它對我來說工作得很好。我想如果我需要這個ID,我可以把它放在extra_data中,但我不確定。

我剛纔分叉與此變更項目:混帳://github.com/marciomr/rails3-jquery-autocomplete.git

1

自從我遇到了這個我自己,我想我會記錄我自己的解決方案爲後人,因爲它不需要編輯寶石的來源。這是爲正式維護的寶石叉:https://github.com/bigtunacan/rails-jquery-autocomplete

您可以直接通過控制器中的自動完成功能塊處理json編碼,我們可以利用它來更改記錄數組。

這就是我們得到的學校的學生去一個獨特的列表的示例:

autocomplete :student, :school do |items| 
    ActiveSupport::JSON.encode(items.uniq{ |i| i["value"] }) 
end 

「項目」是哈希的數組,它默認包含一個標識,標籤和值,所以這隻將唯一的值傳入json編碼器(您選擇的)。