我正在执行搜索,其中传递了两个隐藏参数:
: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)
我试过脱衣舞!但它不会直接在参数上工作。
我的搜索表单:
<%= 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>