Thinking Sphinx Global Search All Models
I'm running sphinx, thinking sphinx and have a basic ordering system with companies, users, order, notes, comments amongst others.
TS is running fine, searching in i开发者_运维技巧ndividual models and their nested resources is working brilliant.
What I wanted to do is have a global search form in my header (application.html.erb). The problem is where my form's posting to.
<%= form_tag companies_path, :method => 'get', :id => "companies_search" do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
Obviously this works for my companies and nested resources. If I put it in my header, any result will be returned on my company layout.
Is it possible to create a dynamic alternative to companies_path?
Or, should I create some special layout?
What does everyone else do here?
I'd create a separate action for handling global searches - or at least, separate routing back to a reusable search action, and have that action detect whether it's a global search, company search, etc.
As for the header - do you always want it global? Because that's easy enough - just use the global/generic search action for the form constantly. If you want it done dynamically, though - defaulting to the context of the models if appropriate? - I'd probably opt for a content_for
block, and add the appropriate search form in each of the relevant views.
Of course, that gets messy, so a different way could be to just customise the url via a helper that looks at params[:controller] to see what the current context is. No idea how fragile this might get, though.
精彩评论