I’ve got an index page setup that has a sunspot search with faceting setup in the controller, model and view (Gears Model).
I’m trying to place a different search form on the Home page of my application (different view and controller) but I want the the form to re-directed the user to the index page of the Gears Model’s search results. Right now the root page is a static page without a controller..How can I pass the search results and redirect the view?
I might be thinking about this wrong, any help is appreciated.
My Code…
Routes
Outdoor::Application.routes.draw do
resources :gears do
resources :calendars, :only => [:create, :destroy, :edit]
resources :comments, :only => [:create, :destroy]
end
match '/gear', to: "gears#index"
root to: 'pages#home'
end
Controller
class GearsController < ApplicationController
respond_to :html, :json
def index
@search = Gear.solr_search do
fulltext params[:search]
facet (:category_name)
facet (:sub_category_name)
facet (:state)
facet (:city)
facet (:price)
with(:price, params[:price]) if params[:price].present?
with(:state, params[:state]) if params[:state].present?
with(:city, params[:city]) if params[:city].present?
with(:sub_category_name, params[:name]) if params[:name].present?
with(:category_name, params[:categoryname]) if params[:categoryname].present?
paginate(page: params[:page], :per_page => 15)
end
@gears = @search.results
end
end
Home Page View Form (I haven’t changed it to use form helper yet)
<form id="homesearch_form" >
<fieldset>
<input type="text" name="homesearchbox" id="homesearch_textarea" onfocus="if(this.value=='Enter the location you’d like to rent gear')this.value='';" value="Enter the location you’d like to rent gear" onblur="if(this.value=='')this.value='Enter the location you’d like to rent gear'"/>
<input type="submit" id="homesearch_submit" value="" />
</fieldset>
<fieldset id="rentaldate_home" class="form-inline">
<input type="text" name="rentaldate_home_from" id="rentaldate_home_from" onfocus="if(this.value=='From')this.value='';" value="From" onblur="if(this.value=='')this.value='From'" />
<input type="text" name="rentaldate_home_to" id="rentaldate_home_to" onfocus="if(this.value=='To')this.value='';" value="To" onblur="if(this.value=='')this.value='To'" />
</fieldset>
</form>
Thanks for the help.
SOLVED
I changed the form on my home page to the following:
<%= form_tag({:controller => "gears", :action => "index"}, :method => "get", :id => "homesearch_form") do %>
<%= text_field_tag :search, params[:search], id: 'homesearch_textarea' %>
<%= submit_tag "", :name => nil, id: 'homesearch_submit' %>
<% end %>
From what I understand, you want to have a search form on your index page but the search result not be presented on the index page but on the gears index page? That should be really simple, just include the same form in this page, preferably with a partial to not copy code:
as in http://railscasts.com/episodes/278-search-with-sunspot