Hi,
I am trying to code a simple search, that will search my database for
something like what ever the user enters.
I can get the form to render ok, but am having some problems when
submitting the test search. I’m wondering if my understanding of this
code, which I have put together after reading several different posts on
the subject is incorrect.
I have commented what I believe is happenning, so hopefully someone can
correct me.
Hi,
I’ve got some search code working now that puts the results on the same
page. Trouble is the view was generated with scaffolding.
The view contains a table that displays the search results, but it is
also displaying all the resources in the DB before a search is run. How
can I add something like an if statement to only loop through @resources
when a search has been performed? Can I embed it in the view, or should
it be in the controller or model?
I would put this in your controller. And before the search runs, you
might want to swap in your search form for the table, so it’s
perfectly clear what to do.
def search
if params[:q] @resources = [your search logic here]
else
render :partial => ‘search_form’
end
end
Walter
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.