Search code examples
ruby-on-railsrubyransack

Rails + Ranscaker, How to hide empty params in url


I have created search forms using Ransack, but I have a problem. When I leave input empty I would like form not to send these inputs, so it won't appear in my parameters.

Right now when i leave filters empty my form

They anyway apear in my url params

url

I am affraid that when I add many more filters it would cause problems

How could I get rid of that? I understand that I am passing '' in that input.

controller:

class RecordsController < ApplicationController
  def index
    @q = Record.ransack(params[:q])
    @records = @q.result(distinct: true)
  end

form:

<%= search_form_for @q do |f| %>
  <%= f.label :title_cont %>
  <%= f.search_field :title_cont %>
  <%= f.submit %>
<% end %>

Solution

  • I have managed to achieve my goal by using jQuery:

    $(document).on('submit','form#form_id',function(){
      $(this)
        .find('input,select')
        .filter(function () {
          return !this.value;
        })
        .prop('name', '');
    });