I have a 2.3.11 Rails app, and I would like to add a fulltext search to be able to search on the 'articles' content and title. I am using Thinking-sphinx for this purpose. I just want to be able to click on the search link_to '/advanced_search' (on the home page) and have the search form come up, then once the search form is submitted have another view search_results.html.erb display a list of matching article(s).
Problem: I found out that I really don't understand how views are actually rendered. I'm also not sure I got my routing correct. I've tried different things, but for some reason all the examples I found out there are different.
So I have a search form /controllers/articles_controller/advanced_search :
<% form_tag :controller => 'articles', :action => 'search', :method => 'get' do %>
<%= text_field_tag :search, params[:search], :id => 'search_field' %>
<%= submit_tag 'Search', :name => nil %>
<% end %>
And I have a second view to display the search results, /controllers/articles_controller/search_results.html.erb:
<% @articles.each do |article| %>
<li><%= link_to article.name, :action => 'show', :id => article.id %></li>
<li><%= article.archived? %></li>
<% end %>
This my model where I define the index for sphinx:
class Article < ActiveRecord::Base validates_presence_of :name #rest of validation and associations here
define_index do indexes summary indexes :name end end
This my controller and new search action /controllers/articles_controller:
class ArticlesController < ApplicationController
load_and_authorize_resource
def index @articles = Article.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @articles }
format.json { render :json => @articles }
end
end
def search @articles = Article.search params[:search]
respond_to do |format|
format.html #
format.xml { render :xml => @articles }
format.json { render :json => @articles }
end
end end
In my routes.rb I have this line, which is probably not correct, but when I take it out, I get the routing error 'No route matches "/advance_search" with {method => get}
map.connect 'advance_search', :controller => "articles, :action => search
I would appreciate any ideas on how to have my views render correctly, assuming that everything else is correct.
one of the issues that messed me up was the fact that nested routing was I think over used. I found a way around this problem for now. I would have loved to leave a straight answer for anyone with the same question, but unfortunately, my solution does not answer this particular question since I went about it a completely different way. I did however notice later that there are other similar questions that were in fact answered.