1

Background

I am creating a search field in my header. This code in my view:

<% if signed_in? %>
    <%=f orm_tag( "/search", method: "get", :class=>"navbar-form navbar-left", :role => "search") do %>
        <div class="form-group">
            <%=t ext_field_tag :q, nil, :class=>"form-control", :placeholder => 'Domain/IP Search' %>
        </div>
        <%=b utton_tag "Search", :type=>'submit', :class=> "btn btn-primary" do %>
        <span class="glyphicon glyphicon-search"></span>
    <% end %>
<% end %>

creates this output:

<form accept-charset="UTF-8" action="/search" class="navbar-form navbar-left" method="get" role="search">
    <div style="display:none">
        <input name="utf8" type="hidden" value="✓">
    </div>
    <div class="form-group">
        <input class="form-control" id="q" name="q" placeholder="Domain/IP Search" type="text">
    </div>
    <button class="btn btn-primary" name="button" type="submit">
        <span class="glyphicon glyphicon-search"></span>
    </button>
</form>

Here is a picture:

search field

The issue

The problem is when I search, I get this in my url:

/search?utf8=✓&q=test&button=

Ideally, I'd like it to be more like:

/search?q=test

tl;dr:

How do I get rid of the button parameter (and hopefully the utf one as well).

Extra info:

routes.rb

resources :search

search_controller.rb

class SearchController < ApplicationController
  def index
    @results = params[:q]
    puts @results.to_s
  end
end

search\index.html.erb

<%=@results.to_s %>

EDIT, answer

Adding this because while the accepted answer helped me get to the solution; it didn't have the exact code for my circumstance. I made these changes:

1) routes:

match '/search',          to: 'search#index',         via: 'post'

2) form in my header:

        <%= form_tag search_path, method: "post", :class => "navbar-form navbar-left", :id=> "SearchForm", :role => "search" do %>
        <div class="form-group">
            <%= text_field_tag :q, nil, :class => "form-control", :placeholder => 'Domain/IP Search' %>
        </div>
        <%= button_tag "Search", :type=> 'submit', :class=> "btn btn-primary" do %>
        <span class="glyphicon glyphicon-search"></span>
    <% end %>
<% end %>
Jeff
  • 4,285
  • 15
  • 63
  • 115
  • 1
    have you considered using POST instead of GET? it will make sure things don't go in the query string. – Taryn East Jul 16 '14 at 05:32
  • Are there additional implications to using POST instead? – Jeff Jul 16 '14 at 05:43
  • 1
    You'd need to change your route to using POST. otherwise... not really. You wouldn't be able to construct a link to it, without also including method = post... – Taryn East Jul 16 '14 at 05:44

3 Answers3

1

If you are not doing with the Utf8 parameter but, Rails is & but its needed. It's to correct some issues in IE's parameter encoding, This parameter was added to forms in order to force Internet Explorer 5, 6, 7 and 8 to encode its parameters as unicode.Its fixes encodeing issue of IE.

Please refer following link:

What is the _snowman param in Ruby on Rails 3 forms for?

Community
  • 1
  • 1
Deepti Kakade
  • 3,053
  • 3
  • 19
  • 30
1

GET

The bottom line, as I wrote in another answer, is that you're using GET:

enter image description here

Essentially, when you submit a GET form, it appends the parameters to your URL, whilst a POST form will append the data to the request body (or somewhere hidden).

The difference is just that the GET request expects a response to be returned immediately, and so you have to pass the parameters in the URL to ensure the server knows how to construct it. The best example being if you use a GET request to load a page.

The POST method is used primarily used for forms, where a response is not expected immediately; thus allowing you to keep the params hidden in the request.


Fix

We've actually created a basic search feature here (the actual search doesn't work for some reason, but the live search does)

The way we did it was as follows:

match 'search(/:search)', :to => 'products#search', :as => :search, via: [:get, :post]

This will firstly allow you to access the search action by typing domain.com/search/your-query-here

In terms of submitting via a form, you'll be able to use JQuery to amend the URL with the input from the text field:

#app/views/elements/_nav.html.erb
<%= form_tag search_path, :method => :post, :id => "SearchForm" do %>
        <%= text_field_tag :search, params[:search], placeholder: 'Search your favourite products or brands', :autocomplete => :off, :id => 'SearchSearch'  %> 
        <%= image_submit_tag('nav_bar/search.png', title: 'Search', class: 'search_submit', data: { "placement" => "bottom" }) %>
<% end %>  

#app/assets/javascripts/application.js
//Search Submit
$(document).ready(function() {
    $('#SearchForm').submit(function(e) {
        e.preventDefault();

        if( $('#SearchSearch').val().length > 0 ) {
            var search_params = '/' + $('#SearchSearch').val().toLowerCase();
        }else{
            var search_params = ''
        }

        window.location.href = $(this).attr('action') + search_params
    });
});
Richard Peck
  • 76,116
  • 9
  • 93
  • 147
0

This parameter is a feature of rails.

It was previously the snowman.

It helps IE to really use utf-8.

Avoid using form_tag and it works:

<form action="<%= search_path %>" method="get" >
  <%= text_field_tag 'query' %>
  <%= submit_tag "Search", :name => nil%>
</form> 

Now style your form as you want

Abdul Baig
  • 3,683
  • 3
  • 21
  • 48