组合多种形式的参数

时间:2014-05-08 16:29:01

标签: jquery ruby-on-rails forms

我有一个搜索表单和过滤器面板来过滤这些搜索结果。

当我搜索一个术语时,我可以成功获得结果但是当我尝试过滤结果时,它会消除原始搜索词。

如何组合所有参数?

搜索表单

<%= form_tag search_path, method: :get, remote: true do %>
                <p>
                  <%= text_field_tag "search[]", params[:search], placeholder: " Search" %>
                </p>
            <% end %>

部分过滤器面板

<%= form_tag search_path, id: "stores", method: :get, remote: true do %>

                <% @colors.each do |color| %>
                    <li>
                      <%= check_box_tag "search[]", color.name.downcase, nil, id: color.name.downcase, rel: color.name.downcase, id: "color_#{color.name.downcase}" %>
                      <div class="color_swatch <%= color.name.downcase %>" title="<%= color.name %>">
                        <label for="color_<%= color.name.downcase %>"></label><%= color.name.downcase %>
                      </div>
                    </li>
                <% end %>

搜索控制器

def search

    # load filters helper
    filters
    # load categories
    categories

    puts "params = #{params}"


    if params[:search].kind_of?(Array)
      params_list = Array(params[:search]).map(&:inspect).join(' ') unless params[:search].nil?
    else
      params_list = params[:search] unless params[:search].nil?
    end


    puts "params_list = #{params_list}"

    @search = Sunspot.search(ItemsDesign) do
      fulltext params_list do
        fields :name, :store
      end

      low_end = params[:low_end] ||= 0
      high_end = params[:high_end] ||= 10000

      with(:price).between(low_end..high_end)
    end

    @products = @search.results

    respond_to do |format|
      format.html
      format.json { render json: @store }
      format.js { render 'search.js.erb'
      }
    end
  end

的javascript

$("#search_wrapper").keypress (e) ->
    console.log "key"
    if e.which is 13
      console.log "enter key"
      $("#search_wrapper form").submit()
      false #<---- Add this line


  $("#filter_menu2 :checkbox").click ->
    # loop through checked checkboxes so that they are all submited
    $("#filter_menu2 :checkbox:checked").each ->
      return

    $("#filter_menu2 form").submit()
    return

在搜索后放置&#d; d params

params = {"utf8"=>"✓", "search"=>["chair"], "controller"=>"new_shop", "action"=>"search"}
params_list = "chair"

在过滤器应用后放置&#39d params

params = {"utf8"=>"✓", "search"=>["classic", "modern"], "controller"=>"new_shop", "action"=>"search"}
params_list = "classic" "modern"

0 个答案:

没有答案