Question

I am following the instructions mentioned here Rails 3: sunspot solr : adding search ability for every page

However it does not seem to work for me. I'm trying to implement a twitter like search functionality where the search bar stays at the top in the navigation bar and one can make a search through any page within in the application and it takes them to the search result page.

When I pass the search query in the browser while in the homepage of posts (views/posts/index.html.erb)

http://localhost:3000/post?utf8=%E2%9C%93&search=test&commit=Search

It returns desired search results. But when I'm in some other page of the application and try to search it forms urls in the address bar like the ones mentioned below and never goes to the search through the search box in the top navigation bar declared in the views/layouts/application.html.erb page.

http://localhost:3000/users/1/following?utf8=%E2%9C%93&search=test&commit=Search

http://localhost:3000/posts/1/favorite?utf8=%E2%9C%93&search=test&commit=Search

Here are the details :-

My model post.rb

searchable do
    text :title, :tag_list 
  end 

My Controller - posts_controller.rb

def index


      if params[:search]

        @search = Post.search do
          fulltext params[:search]
          paginate :page => params[:page], :per_page => 5        
        end

        @posts = @search.results
      else

              @posts = @posts.plusminus_tally({:order => "vote_count ASC"}).page(params[:page]).per(6)   

       end

      respond_to do |format|
        format.html 
        format.json { render json: @posts}
      end
  end

View - layouts/application.rb

 <%= form_tag posts_path, :method => :get do %>
                    <div class="input-append">
                      <%= text_field_tag :search, params[:search] , :class => "span2 search-query", :style => 'width:200px' %>
                      <%= submit_tag "Search", :post => nil , :class => "btn" %>
                    </div>  
                  <% end %> 
Was it helpful?

Solution

I finally figured this out. This needs to be called on every controller of the current page to redirect the search post to its search results page (in this case Posts#index)

Favorite controller & User Controller

 if params[:search]
             search_param = CGI::escapeHTML(params[:search])    

             redirect_to ("/posts?search=#{search_param}&commit=Search") 

            return
       end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top