2015-10-13 19 views
2

我使用jQuery-ui的自動完成功能跟隨本教程(http://www.yoniweisbrod.com/autocomplete-magic-with-rails/),但是當我嘗試使用文本字段進行搜索時,它將路由到控制器的show方法而不是autocomplete_ingredient_name方法。使用Rails 4自動完成控制器中的路由不正確自動完成

下面是我的表單代碼:

<%= form_tag(cocktail_path(1), :method => 'get', :class => "search_form", :remote => true) do %> 
    <%= label_tag(:query, "Choose ingredients:") %> 
    <%= autocomplete_field_tag(:query, params[:query], autocomplete_ingredient_name_cocktails_path, {class: "search-query", placeholder: "", type: "search"}) %> 
    <% @ingredients.each do |ingredient| %> 
    <%= hidden_field_tag "ingredients[]", ingredient.name %> 
    <% end %> 
    <%= submit_tag("Search") %> 
<% end %> 

而且我的控制器。

class CocktailsController < ApplicationController 
    autocomplete :ingredient, :name 

    def index 
    @cocktails = [] 
    @ingredients = [] 
    end 

    def autocomplete_ingredient_name 
    @ingredients = Ingredient.order(:name).where("name LIKE ?", "'%#{params[:query]}%'") 
    respond_to do |format| 
     format.html 
     format.json { 
     render json: @ingredients.map(&:name) 
     } 
    end 
    end 

    def show 
    hash = {} 
    @cocktails = [] 
    @ingredients = Ingredient.all.map {|ingredient| ingredient} 
    @ingredients.select! {|ingredient| ingredient.name.downcase.include?(params[:query])} 
    if params[:ingredients] 
     old_ingredients = [] 
     params[:ingredients].each do |ing| 
     old_ingredients << Ingredient.find_by(name: ing) 
     end 
     cocktails = @ingredients.map {|ingredient| ingredient.cocktails }.flatten 
     old_cocktails = old_ingredients.map {|ingredient| @cocktails << ingredient.cocktails }.flatten! 
     old_cocktails.each do |cocktail| 
     hash[cocktail] = 1 
     end 
     cocktails.each do |cocktail| 
     if hash.has_key?(cocktail) 
      @cocktails << cocktail 
     end 
     end 
     @cocktails = @cocktails.uniq.flatten 
    else 
     @cocktails = @ingredients.map {|ingredient| ingredient.cocktails }.flatten 
    end 

    end 


end 

這裏是來自我的服務器的消息,轉到CocktailsController#show方法,而不是自動完成方法。

Started GET "/cocktails/autocomplete_ingredient_name?term=mi" for ::1 at 2015-10-12 15:32:21 -0500 
Started GET "/cocktails/autocomplete_ingredient_name?term=mi" for ::1 at 2015-10-12 15:32:21 -0500 
Processing by CocktailsController#show as JSON 
Processing by CocktailsController#show as JSON 
    Parameters: {"term"=>"mi", "id"=>"autocomplete_ingredient_name"} 
    Parameters: {"term"=>"mi", "id"=>"autocomplete_ingredient_name"} 
    Ingredient Load (8.6ms) SELECT "ingredients".* FROM "ingredients" 
    Ingredient Load (8.6ms) SELECT "ingredients".* FROM "ingredients" 
Completed 500 Internal Server Error in 38ms (ActiveRecord: 8.6ms) 
Completed 500 Internal Server Error in 38ms (ActiveRecord: 8.6ms) 

TypeError (no implicit conversion of nil into String): 
    app/controllers/cocktails_controller.rb:25:in `include?' 
    app/controllers/cocktails_controller.rb:25:in `block in show' 
    app/controllers/cocktails_controller.rb:25:in `select!' 
    app/controllers/cocktails_controller.rb:25:in `show' 



TypeError (no implicit conversion of nil into String): 
    app/controllers/cocktails_controller.rb:25:in `include?' 
    app/controllers/cocktails_controller.rb:25:in `block in show' 
    app/controllers/cocktails_controller.rb:25:in `select!' 
    app/controllers/cocktails_controller.rb:25:in `show' 

的代碼應該創建一個jQuery UI的下拉式選單,預測您要搜索的內容,但下拉一直沒有出現,它立即返回500錯誤。

任何想法爲什麼這不是路由到正確的方法將不勝感激!

回答

1

這可能是由於路由錯誤,即您的GET "/cocktails/autocomplete_ingredient_name?term=mi"指令由您的/config/routes.rb文件中的錯誤條目處理。

確保處理自動填充過程的路線在處理雞尾酒控制器的顯示操作的路線之前定義。 由於後者通常採用get 'cocktails/:id'的形式,URI的「autocomplete_ingredient_name」部分會受到:id組件的影響,處理將使用所述ID委託給控制器的show操作。

定義了自動完成路由,因爲表單中的autocomplete_ingredient_name_cocktails_path指令會生成格式正確的URI;所以我認爲這只是一個優先問題。

但是,您還有另一個潛在問題:您的自動完成查詢參數在您的請求中是'term',但它是您的控制器操作中的'query'。他們應該有同一個名字。

+0

是的,這解決了它。我只是將該路線移到頂端,並修復了查詢/術語差異,現在它正在工作。謝謝你的幫助! – JasmineF