带有elasticsearch的Select_tag

时间:2016-08-28 19:02:13

标签: ruby-on-rails forms search elasticsearch ruby-on-rails-5

我尝试传递2个搜索选项。第一个[:q]用于访问者的输入文本,另一个来自模型露营" nomdep" (比如英语部分)。当我尝试通过输入进行搜索时,它可以正常工作,但是因为我尝试添加select_tag,我有一个错误

错误 未定义的方法`map'为零: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]为空,搜索不起作用。所以如果我只选择_tag =&gt;没有结果。

如何解决这个问题?

我的完整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>

2 个答案:

答案 0 :(得分:1)

@camping = Camping.all

此变量将为零,除非:q在params中传递给索引操作。 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

答案 1 :(得分:0)

我想对@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('\\+-&|!(){}[]^~*?:/')}])/, '\\\\\1') }\""}
    .join(" AND ")

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

    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 %>