Select_tag 使用弹性搜索

Select_tag with elasticsearch

我尝试传递 2 个搜索选项。第一个 [:q] 用于访问者输入的文本,另一个来自 model camping "nomdep"(如英语部门)。当我尝试通过输入进行搜索时,它是有效的,但是由于我尝试添加 select_tag 我有一个错误

错误 nil:NilClass

的未定义方法“映射”

我迷路了,你有什么想法吗?

对不起我的英语,我是法语。

Home_controler.rb

 def index
      if params[:q].nil?
        "Entrez un mot clef"
      else
        @campings = Camping.__elasticsearch__.search params[:q,:nomdep]
        @camping = Camping.all
    end
    end

def result
      if params[:q].nil?
        @campings = []
      else
        @campings = Camping.__elasticsearch__.search(params[:q]).page(params[:page]).per(14).results
      end
end

查看

<div class="search">
        <%= form_tag(result_path, method: :get) %>
        <%= text_field_tag :q, params[:q], class:"search-query form-control" %>
                    <%= select_tag(:nomdep, options_for_select(@camping)) %>

              <%= submit_tag "Partez", class:"btn btn-danger", name: nil %>
    </div>

编辑 现在我没有任何错误,但如果 [:q] 为空,搜索将不起作用。所以如果我只 select_tag => 没有结果。

如何解决这个问题?

我满home_controller.rb

class HomeController < ApplicationController
  def index
      @camping = Camping.all
      if params[:q].nil?
        "Entrez un mot clef"
      else
        @campings = Camping.__elasticsearch__.search params[:q, :nomdep]
    end
    end

  def result
      if params[:q].nil?
        @campings = []
      else
        @campings = Camping.__elasticsearch__.search(params[:q]).page(params[:page]).per(14).results
      end

end
end

我的观点

<div class="search">
            <%= form_tag(result_path, method: :get) %>
                        <%= select_tag :nomdep, options_from_collection_for_select(@camping, :id, :nomdep), prompt: "Département" %>
                  <%= text_field_tag :q, params[:q], class:"search-query form-control" %>
                  <%= submit_tag "Partez", class:"btn btn-danger", name: nil %>
        </div>
@camping = Camping.all

除非 :q 在参数中传递给索引操作,否则此变量将为 nil。 options_for_select(@camping) 将尝试在此变量上调用 #map 并在未初始化时引发错误。

您应该确保它已初始化。例如,尝试重写您的操作:

 def index
      @camping = Camping.all
      if params[:q].nil?
        "Entrez un mot clef"
      else
        @campings = Camping.__elasticsearch__.search params[:q]
      end
 end

我想对@Baradzed 说声大大的感谢!我们昨天谈过,他找到了一个完美的解决方案!

home_controller.rb

class HomeController < ApplicationController
  def index
      @camping = Departement.all
      if params[:q].blank? || params[:nomdep].blank?
      @campings = Camping.__elasticsearch__.search params[:nomdep]
      else
      @campings = Camping.__elasticsearch__.search params[:q]

    end
  end



  def result

    querystring = params.slice(:nomdep, :other_param, :any_params_except_q_because_we_will_process_q_separately)
    .select{|k,v| v.present?}
    .map {|key, value| "#{key}:\"#{value.gsub(/([#{Regexp.escape('\+-&|!(){}[]^~*?:/')}])/, '\\') }\""}
    .join(" AND ")

    freetext = params[:q]
    freetext.gsub!(/([#{Regexp.escape('\+-&|!(){}[]^~*?:/')}])/, '\\')

    querystring = ["*#{freetext}*",querystring].select{|v| v.present?}.join(" AND ") if params[:q].present?

    if querystring.blank?
    flash[:notice] = "Aucune mots clefs"
    redirect_to action: :index and return
    else
    @campings = Camping.__elasticsearch__.search(
    query: { query_string: {
    query: querystring
    }}).page(params[:page]).per(14).results
    end


        @hash = Gmaps4rails.build_markers(@campings) do |camping, marker|
          marker.lat camping.latitude
          marker.lng camping.longitude
          marker.infowindow render_to_string(:partial => "/campings/infowindow", :locals => { :camping => camping})
          marker.picture ({
            "url" => "http://avantjetaisriche.com/map-pin.png",
            "width" =>  29,
            "height" => 32})
        end
end
end

查看

 <div class="search">
            <%= form_tag(result_path, method: :get) %>
                        <%= select_tag :nomdep, options_from_collection_for_select(@camping, :nomdep, :nomdep), prompt: "Département" %>
                  <%= text_field_tag :q, params[:q], class:"search-query form-control" %>
                  <%= submit_tag "Partez", class:"btn btn-danger", name: nil %>