I added a search bar to my header, so every page in the application has one. However, the search bar only works on the home page and index pages. When I go to my contact page (just an example) it doesn't work. I have tried redirecting to my search URL, but nothing seems to work. Any suggestions?
search_controller.rb (controller)
class SearchController < ApplicationController
def index
@search = Sunspot.search [Dairy, Drink] do
fulltext params[:search]
end
if @search.results.any?
@results = @search.results
else
return redirect_to request_path
end
end
end
search.rb (model)
class Search < ActiveRecord::Base
attr_accessible :title
searchable do
autosuggest :search_title, :using => :title
end
end
_searchbarheader.html.erb (search bar view)
<%= form_tag search_index_path, :method => :get do %>
<%= text_field_tag :search, params[:search], style:"width:300px; height:30px;" %>
<%= submit_tag "Search!", :name => nil, class: "btn btn-default"%>
<% end %>
_header.html.erb (header view)
<div class="container-fluid">
<%= render 'layouts/brand' unless @skip_brand %>
<form class="navbar-form navbar-left">
<%= render 'layouts/searchbarheader' unless @skip_header %>
</form>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav navbar-right">
<% if user_signed_in? %>
<li><%= link_to current_user.name%></li>
<li><a class="glyphicon glyphicon-cog" href="<%=edit_user_registration_path%>"></a></li>
<li><%= link_to "Logout", destroy_user_session_path, method: :delete %> </li>
<%else%>
<li><%= link_to "Login", new_user_session_path %></li>
<li><%= link_to "Sign up", new_user_registration_path %></li>
</ul>
<%end%>
</div>
My searchbar is rendered into the header, and header rendered into the app. Thank you for any help. Still learning how rails and solr works together.
Remove the form tags in _header.html.erb wrapping the search bar. The form_tag helper in _searchbarheader.html.erb will render form tags as well. So, as you have it now, your HTML output will have nested forms. I'm not sure that's the issue, but it's incorrect HTML so could cause problems depending on what else is on the page, e.g., other forms.