Note that there are some explanatory texts on larger screens.

plurals
  1. POrails search form that works on all pages of application
    text
    copied!<p>I have a search form which appears on all pages because I want someone to be able to search and be redirected to search results regardless of where they are on the site. To achieve this i placed the form in a partial which i included in the application layout. It looks like this</p> <pre><code>&lt;form class="form-search center"&gt; &lt;%=form_tag search_url, method: :get do%&gt; &lt;%=text_field_tag :query, params[:query] ,{:class=&gt;"input-xxlarge search-query"}%&gt; &lt;button type="submit" class="btn"&gt;Search&lt;/button&gt; &lt;%end%&gt; &lt;/form&gt; </code></pre> <p>I have created a controller called SearchResults with an index action to display the results. The named route looks like this</p> <pre><code>match '/search', to: 'search_results#index' </code></pre> <p>The search works perfectly fine on the search page, but cannot work anywhere else.My index action looks like this</p> <pre><code>class SearchResultsController &lt; ApplicationController def index @restaurants = Restaurant.text_search(params[:query]).page(params[:page]).per_page(3) end end </code></pre> <p>I want to be able to redirect to this action whenever i carry out a search regardless of where i am in the application.The text_search method is defined in the restaurant model like so</p> <pre><code>def self.text_search(query) if query.present? where("restaurant_name ilike :q or description ilike :q", q: "%#{query}%") else scoped end </code></pre> <p>It doesn't work in that nothing happens when i search but if i go to the search page it returns the results.</p> <p>I have noticed something interesting. When I search from the home page for a term like eats. The url looks like this <code>localhost:3000/?utf8=✓&amp;query=eats</code> and when I search from the real search page which works perfectly it looks like this <code>localhost:3000/search?utf8=✓&amp;query=eats</code>. How can i get it point to the latter url regardless of where am searching from?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload