============ Rails controller: class SearchController < ApplicationController layout "home" # your search string looks like this: # # www.[site_not_yet_public].com/search?category=Track&term=funky # # this code uses the category to build an instance variable which holds # the Ferret search results in that category. for instance if you go # in the script/console and type "Track".constantize.find_all, you'll # get the exact same results you'd get if you typed Track.find_all. so # by using constantize, we get Whatever.find() without having to create # four separate methods for searching on Track, Beat, User, or Profile # objects. instead this code just dynamically obtains the model and then # searches for the term, on the model (acts_as_ferret attaches search # methods to the model). it first filters the category var to prevent # hackers from doing either SQL or Ruby injection attacks. def index @term = params[:term] || '' @category = params[:category] if not %w{ Track Beat User Profile }.include? @category redirect_to "/" else instance_variable_set("@#{@category.downcase.pluralize}", (@category.constantize.find_by_contents @term)) end end end ============ Main Rails view: <%= render_partial @category.downcase.pluralize %> <%= render_partial "options" %> ============ Options partial: <% for word in %w{ tracks beats users profiles } %> <% if eval("@#{word}").nil? %> <%= link_to "search similar #{word}", :action => "index", :term => @term, :category => word.capitalize.singularize %>
<% end %> <% end %> ============ Typical model-specific partial: <% for beat in @beats %> <%= beat.title %>
<% # model-specific UI or code %> <% end %>