I am implementing search where I am passing two hidden parameters:
:sort and :direction
When I do the search I get:
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
Checking the params, I see I get unpermitted parameters utf8 and most importantly I am getting
{:value => "\rentalminimum"\} and not {:value => "rentalminimum"}
How should I remove these parameters as %7B%3Avalue%3D%3E%22
from my search url. In other words how can I santize my params to include only the search params and direction and sort column name?
Resource.search(params)
I tried strip! but it won't work directly on params.
My 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 %>
Controller action:
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
Sortable helper:
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
Sorting links:
<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>
There's a few things I think may have been missed here...
hidden_field_tag
introduces an<input>
element on the page that the user can't see. It doesn't affect how its value gets sent back to the server. I don't think yours are actually doing anything at all.sortable
method) bypass the form and its inputs anyway and just link you to a page. Theparams
it refers to will be those that were sent with the request that loaded the page (which may be what you want, of course).GET
requests send their parameters back in the URL. If you want that not to happen at all, you'll need to send the request via a different HTTP method -POST
is probably most appropriate. I'm not sure if links can do this - they might acceptmethod: :post
as an option - or if you need to use a form's submit button.However, you say all you want is to remove the
value
rubbish from the URL? I think that comes down to thehidden_field_tag
arguments. If I remember rightly, the second one is expected to be the value of the field, not an options hash. Try:As for the
utf8
parameter, there is a reason it's there.