Search code examples
ruby-on-railsruby-on-rails-4elasticsearchtextboxsearchkick

How to keep search box input in search box when submitted Elasticsearch


Using elastic search with searchkick on a rails 4 app.

I would like the input(query) the user types into the search box to stay there after the submit button is clicked and the page is redirected. Every page of my app has a search bar so keeping the users input in the box would be great. As of now the search box clears after submitting.

Having some trouble doing this. Have tried a few different methods but nothing is working. Here is my search bar view:

<%= form_tag articles_path, method: :get do %>
 <p>
  <%= text_field_tag :query, params[:query], style:"width:550px; height:34px;", :autofocus => true, class: "form-control" %><br>
  <%= submit_tag "Let's Find Out!", :name => nil, class: "btn btn-default", style: "margin-top:6px; margin-right: 10px;" %><%= submit_tag "Most Popular", :name => nil, class: "btn btn-default", style: "margin-top:6px; margin-left: 10px;" %>
 </p>
<% end %>

Any suggestions would be greatly appreciated.


Solution

  • A couple quick suggestions:

    1. drop the params[:query] in the session or a cookie and insert into your text_field_tag as the placeholder or value property. The obvious issue is that you'd manually have to clear it when necessary.
    2. pass the params[:query] as a query string and grab it that way inserting into the text_field_tag as above.