rails search form that works on all pages of application -
i have search form appears on pages because want able search , redirected search results regardless of on site. achieve placed form in partial included in application layout. looks this
<form class="form-search center"> <%=form_tag search_url, method: :get do%> <%=text_field_tag :query, params[:query] ,{:class=>"input-xxlarge search-query"}%> <button type="submit" class="btn">search</button> <%end%> </form> i have created controller called searchresults index action display results. named route looks this
match '/search', to: 'search_results#index' the search works fine on search page, cannot work anywhere else.my index action looks this
class searchresultscontroller < applicationcontroller def index @restaurants = restaurant.text_search(params[:query]).page(params[:page]).per_page(3) end end i want able redirect action whenever carry out search regardless of in application.the text_search method defined in restaurant model so
def self.text_search(query) if query.present? where("restaurant_name ilike :q or description ilike :q", q: "%#{query}%") else scoped end it doesn't work in nothing happens when search if go search page returns results.
i have noticed interesting. when search home page term eats. url looks localhost:3000/?utf8=✓&query=eats , when search real search page works looks localhost:3000/search?utf8=✓&query=eats. how can point latter url regardless of searching from?
why don't proper routing entry search controller?
resources :search_results, :only => :index and in search form let post searches_path. don't have fiddle around :match routing.
edit
i noticed get form. technically doesn't matter, it's not clean. maybe, this:
resources :searches_results post :query end and in controller have query function.
edit 2
and think real problem here having have form tag in form tag. first form tag gets evaluated , points /. therefore works in controller itself, not anywhere else.
Comments
Post a Comment