lundi 26 janvier 2015

Search across multiple controllers


Vote count:

0




So I'm working on a Rails project in which I have a frontend interacting with multiple controllers to retrieve information. I have instructors and courses controllers and I am trying to implement a search feature where I can select which controller to search from using a drop down box, and when I click the search button, it will only search the selected controller.


My search box on the home page looks like this:


http://ift.tt/1Bi2Srm (SO won't let me embed an image)


The code to make it is below:



<%= form_tag(params[:option], :method => "get", id: "search-form") do %>
<%= select(:option,options_for_select([['Instructors', 'instructors'], ['Courses', 'courses'],['Departments','departments']])) %>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil, html: {class: "button button-red"}%>
<% end %>


Special note on line 1 where I use params[:option], this populates with the information from the 'option' field, so if I select 'Instructors', the option becomes instructors.


Problem: On the homepage when I enter a name and click search the url that is created is as follows:


http://ift.tt/1zRFw0D


and it takes me right back to the homepage. I can see in the debug information, that it isn't even getting to the right controller, it's only looking at the static pages controller.


If I click the search button again it brings me to the correct page with the correct search results:


http://ift.tt/1Bi2Sro


I'm using Solr to do the searching, and I have everything working properly except this pathing issue which requires multiple clicks. My controller code for instructors index looks like this:



def index
if params[:search] != ''
@search = Instructor.search do
fulltext params[:search]
end
@instructors = @search.results

else
@instructors = Instructor.all
end
end


I feel like I am making an architectural mistake somewhere, and any input would be appreciated.



asked 40 secs ago







Search across multiple controllers

Aucun commentaire:

Enregistrer un commentaire