我使用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錯誤。
任何想法爲什麼這不是路由到正確的方法將不勝感激!
是的,這解決了它。我只是將該路線移到頂端,並修復了查詢/術語差異,現在它正在工作。謝謝你的幫助! – JasmineF