使用Rails 4自动完成功能在控制器中路由不正确

时间:2015-10-13 01:51:21

标签: jquery-ui ruby-on-rails-4 jquery-ui-autocomplete

我跟随本教程(http://www.yoniweisbrod.com/autocomplete-magic-with-rails/)使用jQuery-ui的自动完成功能,但是当我尝试使用文本字段进行搜索时,它会路由到控制器' s show method而不是autocomplete_ingredient_name方法。

这是我表单的代码:

<%= 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 个答案:

答案 0 :(得分:1)

这可能是由于路由错误,即您的GET "/cocktails/autocomplete_ingredient_name?term=mi"指令由/config/routes.rb文件中的错误条目处理。

确保处理自动完成过程的路线在处理鸡尾酒控制器的show动作的路线之前定义。 由于后者通常采用get 'cocktails/:id'形式,因此&#39; autocomplete_ingredient_name&#39; URI的一部分受:id组件的影响,并且处理委托给控制器的show操作,并带有所述ID。

定义了自动完成路由,因为表单中的autocomplete_ingredient_name_cocktails_path指令会生成格式正确的URI;所以我认为这只是一个优先问题。

但是,您还有另一个潜在问题:您的自动填充查询参数是&#39; term&#39;在您的请求中,但它是查询&#39;在你的控制器动作中。他们应该有同名的名字。