Search code examples
ruby-on-railselasticsearchsearchkick

How to clear the search results in a Rails app?


I'm a Rails noob, and am looking for assistance in clearing the results of a search.

I've built a sample application which lists cafes, and have implemented searching using elasticsearch and searchkick. The search function appends parameters to the URL as in http://localhost:3000/cafes?search=sydney&commit=Search and is working correctly.

Now I'd like to add a button to clear the search results:

enter image description here

The Clear button's link is to the cafes_path (http://localhost:3000/cafes), so it basically reloads the page without the search parameters:

= form_tag cafes_path, :method => :get do
      = text_field_tag :search
      = submit_tag value = "Search"
      = link_to cafes_path
  1. Is that an acceptable way to clear the search?
  2. Is it an acceptable Rails approach to use JavaScript logic to enable/disable the Clear button, based on what's in the search box?

Please let me know if you need to see more of the model or controller code to make sense of the question.


Solution

    1. This way is pretty standard
    2. sure, use javascript - or you can also google for other ways it's been done and see what they did.