为什么我在我的搜索网址中出现奇怪的参数?
问题描述:
我在执行的搜索中,我传递两个隐藏的参数:为什么我在我的搜索网址中出现奇怪的参数?
: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
检查PARAMS,我看我得未经许可参数UTF8,最重要的我越来越
{:value => "\rentalminimum"\} and not {:value => "rentalminimum"}
我应该如何消除这些参数%7B%3Avalue%3D%3E%22
从我的搜索网址。换句话说,我怎样才能让我的参数变得只包含搜索参数和方向并对列名进行排序呢?
Resource.search(params)
我试过了!但它不会直接在params上工作。
我searchform:
<%= 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>
答
有几件事情,我认为可能已经错过了这里...
-
hidden_field_tag
介绍页面,用户无法看到上<input>
元素。它不会影响其值如何返回到服务器。我不认为你的确在做任何事情。 - 链接(在
sortable
方法中)无论如何绕过表单及其输入,只是将您链接到页面。它引用的params
将是那些与加载页面的请求一起发送的(当然,这可能是您想要的)。 -
GET
请求将其参数发送回URL。如果您不希望发生这种情况,则需要通过其他HTTP方法发送请求 -POST
可能是最合适的。我不确定链接是否可以这样做 - 他们可能会接受method: :post
作为选项 - 或者如果您需要使用表单的提交按钮。
但是,你说你想要的只是从URL中删除value
垃圾?我认为即归结为hidden_field_tag
的论点。如果我没有记错的话,那么第二个应该是该字段的值,而不是选项散列。尝试:
<%= hidden_field_tag :direction, params[:direction] %>
对于utf8
参数,there is a reason it's there。
您可能需要包含您的视图和控制器代码。 – PJSCopeland
添加了所有必要的代码 –