I am quite new to rails, and encountered problem when implementing a search bar in my application. This search bar exists in every page of my application, however it only works when I search in the root page. If I click the search button in other pages, the page will not be redirected to home/index and perform search.
Below is my controllers/home_controller.erb (root_path is home#index):
class HomeController < ApplicationController
def index
if params[:display]
@promos = Promo.where(category: params[:display]).order(created_at: :desc)
elsif params[:search]
@promos = Promo.search(params[:search]).order(created_at: :desc)
else
@promos = Promo.all.order(created_at: :desc)
end
end
end
Below is in my view/application.html.erb
<form class="search_bar">
<div class="input-group">
<%= form_tag(root_path, :method => "get", :controller => "home" ) do %>
<%= text_field_tag :search, '', placeholder: "Search Promos", class: "form-control search-input" %>
<%= button_tag(type: 'submit', class: "btn btn-default") do %>
<i class="glyphicon glyphicon-search"></i>
<% end %>
<% end %>
</div>
</form>
Can anyone help me with this problem? I mainly want to achieve redirecting to the root page and performing the search action. However, if there is any other options of implementing this logic while achieving the same effect, I would be very appreciated to learn that too.
Thanks!!