2015-02-10 41 views
0

我想實現自動完成表單數錯誤下面這個例子How to set up jquery ui autocomplete in rails的參數-trying實現自動完成與jQueryUI的

當我去http://localhost:3000/initiatives/10.json?term=Ion我得到一個「的參數1錯誤號0」 。

在我的舉措#表明我有以下代碼:

if params[:term] 
    Error points to this line - @people = User.all(:all, :conditions => ['name LIKE ?', "#{params[:term]}%"]) 
else 
    @people = User.all 
end 

respond_to do |format| 
    format.html 
    format.json { render :json => @people.to_json } 
end 

我使用PostgreSQL和我想查詢是不是該數據庫製作。

這是自動完成的腳本:

<script type="text/javascript"> 
    $(function() { 

    $('#delegatee').autocomplete({ 
      minLength: 2, 
      source: '<%= json_path %>', 

      focus: function(event, ui) { 
       console.log("xxxxxx"); 
       console.log(ui); 

       $('#delegatee').val(ui.item.name); 
       return false; 
      }, 

      select: function(event, ui) { 

       $('#delegatee').val(ui.item.name); 
     $('#delegatee_id').val(ui.item.id); 
       return false; 
      } 
     }) 
     .data("uiAutocomplete")._renderItem = function(ul, item) { 
      return $("<li></li>")  
       .data("item.autocomplete", item) 

       .append("<a>" + item.name + "</a>") 
       .appendTo(ul); 
     }; 
    }); 

</script> 

「<%= json_path%>」 導致/initiatives/:id.json這使得數據..正確,我認爲。

當我嘗試的形式出它給了我在控制檯中,我相信這是我上面描述的一個500內部服務器錯誤,並指出我此行的jQuery腳本:xhr.send((options.hasContent && options.data) || null);

我m使用Rails 4.2.0.beta4和Ruby 2.0.0p195。

回答

0

在Rails> 4.0中,ActiveRecord's .all在傳遞過程中沒有采用多個參數,因此出現Wrong number of arguments 1 for 0錯誤。

你想設置的查詢將只是:

@people = User.where('name LIKE ?', "#{params[:term]}%") 

希望它能幫助!