2017-09-04 57 views
0

我在執行的搜索中,我傳遞兩個隱藏的參數:爲什麼我在我的搜索網址中出現奇怪的參數?

: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> 
+1

您可能需要包含您的視圖和控制器代碼。 – PJSCopeland

+0

添加了所有必要的代碼 –

回答

0

有幾件事情,我認爲可能已經錯過了這裏...

  • 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

相關問題