为什么我的搜索网址中出现了奇怪的参数?

时间:2017-09-04 21:00:28

标签: ruby-on-rails search utf-8 parameters search-form

我正在实现搜索,我传递了两个隐藏参数:

:sort and :direction

当我进行搜索时,我得到:

http://localhost:3000/resource?utf8=%E2%9C%93&direction=%7B%3Avalue%3D%3E%22asc%22%7D&sort=%7B%3Avalue%3D%3E%22rentalminimum%22%7D&startdate=&near=tempe&radius=&min=&max=&commit=Search

检查参数,我看到我得到了未经许可的参数utf8,最重要的是我得到了

{:value => "\rentalminimum"\}  and not {:value => "rentalminimum"}

如何将这些参数删除为%7B%3Avalue%3D%3E%22 来自我的搜索网址。换句话说,我怎样才能使我的参数仅包括搜索参数和方向以及对列名称进行排序?

Resource.search(params)

我试过条带!但它不能直接用于params。

我的搜索表单:

<%= bootstrap_form_for listings_path, :method => 'get' do %>

        <%= hidden_field_tag :direction, :value => params[:direction] %>
        <%= hidden_field_tag :sort,:value => params[:sort] %>



        <div class= "col-sm-12 col-lg-12 col-md-12" style = "margin: auto;">
            <h6 style = "color:#7C064D;"><strong> PICK A DATE  <span class="glyphicon glyphicon-calendar"></span></strong>
            <%= date_field_tag :startdate, params[:startdate], placeholder: 'DATE' %>           
            </h6>
        </div>  

        <div class= "col-sm-12 col-lg-12 col-md-12" style = "margin: auto;">    
        <p>     
            <%= text_field_tag :near, params[:near], placeholder: ' Destination' %>
            <%= text_field_tag :radius, params[:radius], placeholder: ' Search Radius' %>
        </p>
        </div>      
        <div class= "col-sm-12 col-lg-12 col-md-12" style = "margin: auto;">    
        <p>     
            <%= text_field_tag :min, params[:min], placeholder: ' Minimum Rate Per Hour' %>
            <%= text_field_tag :max, params[:max], placeholder: ' Maximum Rate Per Hour' %>
        </p>
        </div>

        <div class= "col-sm-12 col-lg-12 col-md-12" style = "margin-top: 10px;">        
            <%= submit_tag "Search", class: "btn btn-info", style: "width: 40%; background-color: #E20049; border: #e20049;" %>
            <%= link_to 'View All', root_path, class: "btn btn-info", style: "width: 40%; background-color: #E20049; border: #e20049;" %>
        </div>

        <!-- <div class= "col-sm-6 col-lg-6 col-md-6" style = "margin-top: 10px;">      

        </div> -->


    <% end %>

控制器操作:

def index
        if params.present?      
          flash[:notice] = "Please see Listings below"
          @listingssearch =  Listing.search(params)        
        else
          @listingssearch =  Listing.all      
        end

        @listingsboats = @listingssearch.where(:vehicletype => 'Boat').order(sort_column + " " + sort_direction).paginate(:page => params[:page], :per_page => 30)

        # @listingsrvs = Listing.search(params)
        @listingsrvs = @listingssearch.where(:vehicletype => 'RV').order(sort_column + " " + sort_direction).paginate(:page => params[:page], :per_page => 30)

        # .page(params[:page]).per_page(4)    
      end

可排序助手:

 def sortable(column, title = nil)
      title ||= column.titleize
      css_class = column == sort_column ? "current #{sort_direction}" : nil
      direction = column == sort_column && sort_direction == "asc" ? "desc" : "asc"      
      # link_to title, request.params.merge({:sort => column, :direction => direction, :page => nil}), {:class => "css_class" }
      link_to title, params.permit(:min, :max, :radius, :startdate, :near).merge({:sort => column, :direction => direction, :page => nil}), {:class => "css_class" }
    end

排序链接:

<div class= "col-sm-12 col-lg-12 col-md-12" style = "text-align: center; padding: 10px;">

    <div class= "col-sm-3 col-lg-3 col-md-3" style = " padding: 5px;">
        <%= sortable "rentalminimum", "SORT BY RENTAL MINIMUM" %>
    </div>

    <div class= "col-sm-3 col-lg-3 col-md-3" style = " padding: 5px;">
        <%= sortable "rateperhour", "SORT BY RATE PER HOUR" %>
    </div>  

    <div class= "col-sm-3 col-lg-3 col-md-3" style = " padding: 5px;">
        <%= sortable "length", "SORT BY LENGTH" %>
    </div>
    <div class= "col-sm-3 col-lg-3 col-md-3" style = " padding: 5px;">
        <%= sortable "sleeps", "SORT BY SLEEPS" %>  
    </div>
</div>

1 个答案:

答案 0 :(得分:0)

我认为这里有一些事情可能会错过......

  • hidden_field_tag在页面上引入了用户看不到的<input>元素。它不会影响其值如何发送回服务器。我认为你们其实并没有做任何事情。
  • 链接(在sortable方法中)无论如何都会绕过表单及其输入,只需将您链接到页面即可。它引用的params将是与加载页面的请求一起发送的(当然可能是您想要的)。
  • GET请求将其参数发回URL中。如果您希望根本不发生这种情况,则需要通过不同的HTTP方法发送请求 - POST可能是最合适的。我不确定链接是否可以执行此操作 - 他们可能会接受method: :post作为选项 - 或者如果您需要使用表单的提交按钮。

但是,你说你想要的只是从网址中删除value垃圾?我认为 归结为hidden_field_tag个参数。如果我没记错的话,第二个应该是该字段的值,而不是选项哈希值。尝试:

<%= hidden_field_tag :direction, params[:direction] %>

至于utf8参数,there is a reason it's there