Hi, Apologies if this is a really silly question, but I cannot seem to quite work out the answer. I would like to implement a search form, to allow me to search across different types of records, ideally using the controllers existing search function. So what i have done, is create a partial called app/views/common/_searchmenu.rhtml Which looks something like this: <code> <%= start_form_tag :controller => ''news'', :action => ''search'' %> <div id="sideSearchBox"> <div id="searchHeader">Search</div> <div id="sideSearchBoxContent"> <input name="params" type="text" size="15" /> <input type="submit" name="Submit" value="GO" /><br/> <table> <tr><td valign="middle">Articles :</td><td valign="middle"><input type="radio" name="search" checked="true" value="1"/></td></tr> <tr><td valign="middle">Bookmarks :</td><td valign="middle"><input type="radio" name="search" value="5"/></td></tr> <tr><td valign="middle">RFC''s :</td><td valign="middle"><input type="radio" name="search" value="2"/></td></tr> <tr><td valign="middle">People :</td><td valign="middle"><input type="radio" name="search" value="3"/></td></tr> <tr><td valign="middle">Organisations :</td><td valign="middle"><input type="radio" name="search" value="4"/></td></tr> </table> </div> </div> <%= end_form_tag %> </code> My news controller will accept the parameters and execute the search ok. But i would also like to search using search functions in other controllers So, I created a new controller called search_controller.rb which could accept the form on submit, and re-direct to the appropriate controller from there, using this simplistic code: <code> class SearchController < ApplicationController layout AppConfig::CONFIG[:Default_Layout] def search @query = @params["params"] # 1 = Articles # 2 = RFC # 3 = People # 4 = Organisations # 5 = Bookmarks case @params["search"] when 1 news_search render_action ''news_search'' end end def news_search flash[''notice''] = ''Params'' @query = @params["params"] @news = News.search(@query) end end </code> But i do not think i have fully understood what render_action and redirect_to do. Can anyone advise what I should be doing, or at least point me in the right direction please. Cheers Geoff
Hi Geoff, Could you do it like this def search @query = @params["params"] # 1 = Articles # 2 = RFC # 3 = People # 4 = Organisations # 5 = Bookmarks case @params["search"] when 1 @results = News.search[params] when 2 @results = RFC.search[params] when 3 @results = People.search[params] etc... end end Best, Alan Boyce
Thanks for that, but how would I use the existing view to display the results, or would I have to copy these into the search view folder? Cheers Geoff Alan Boyce wrote:>Hi Geoff, > >Could you do it like this > >def search > @query = @params["params"] > # 1 = Articles > # 2 = RFC > # 3 = People > # 4 = Organisations > # 5 = Bookmarks > case @params["search"] > when 1 > @results = News.search[params] > when 2 > @results = RFC.search[params] > when 3 > @results = People.search[params] > etc... > end > end > >Best, > >Alan Boyce >_______________________________________________ >Rails mailing list >Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >http://lists.rubyonrails.org/mailman/listinfo/rails > > >
Geoff, create a new view called search.rhtml (file name matches the function named search) In the search.html have something like <% for result in @results %> <%=h result.title %> <% end %> I would also probably get rid of the drop down selector box and just put a search box and then in the search function do @news_results = News.search[] @people_results = People.search Then in the search.rhtml file do a listing of each like <h3>Results in News</h3> <% for news in @news_results %> #display link, abstract, etc. <h3>Results in People</h3> <% for people in @people_results %> #display link, abstract, etc. On 9/25/05, Geoff <geoff-hL7hnT9RFJ9RuSCTpLpCrg@public.gmane.org> wrote:> Thanks for that, but how would I use the existing view to display the > results, or would I have to copy these into the search view folder? > > Cheers > > Geoff > > Alan Boyce wrote: > > >Hi Geoff, > > > >Could you do it like this > > > >def search > > @query = @params["params"] > > # 1 = Articles > > # 2 = RFC > > # 3 = People > > # 4 = Organisations > > # 5 = Bookmarks > > case @params["search"] > > when 1 > > @results = News.search[params] > > when 2 > > @results = RFC.search[params] > > when 3 > > @results = People.search[params] > > etc... > > end > > end > > > >Best, > > > >Alan Boyce > >_______________________________________________ > >Rails mailing list > >Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > >http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
I thought that might me the answer. Oh well I suppose if I ensure the other search.rhtml (from Person, Bookmarks etc) are using paritals, then this will cut down on some duplicated code <sigh> Cheers Geoff Alan Boyce wrote:>Geoff, > >create a new view called search.rhtml (file name matches the function >named search) > >In the search.html have something like > ><% for result in @results %> > <%=h result.title %> ><% end %> > >I would also probably get rid of the drop down selector box and just >put a search box and then in the search function do >@news_results = News.search[] >@people_results = People.search > >Then in the search.rhtml file do a listing of each like > ><h3>Results in News</h3> ><% for news in @news_results %> > #display link, abstract, etc. > ><h3>Results in People</h3> ><% for people in @people_results %> > #display link, abstract, etc. > >On 9/25/05, Geoff <geoff-hL7hnT9RFJ9RuSCTpLpCrg@public.gmane.org> wrote: > > >>Thanks for that, but how would I use the existing view to display the >>results, or would I have to copy these into the search view folder? >> >>Cheers >> >>Geoff >> >>Alan Boyce wrote: >> >> >> >>>Hi Geoff, >>> >>>Could you do it like this >>> >>>def search >>> @query = @params["params"] >>> # 1 = Articles >>> # 2 = RFC >>> # 3 = People >>> # 4 = Organisations >>> # 5 = Bookmarks >>> case @params["search"] >>> when 1 >>> @results = News.search[params] >>> when 2 >>> @results = RFC.search[params] >>> when 3 >>> @results = People.search[params] >>> etc... >>> end >>> end >>> >>>Best, >>> >>>Alan Boyce >>>_______________________________________________ >>>Rails mailing list >>>Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>>http://lists.rubyonrails.org/mailman/listinfo/rails >>> >>> >>> >>> >>> >>_______________________________________________ >>Rails mailing list >>Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>http://lists.rubyonrails.org/mailman/listinfo/rails >> >> >> >_______________________________________________ >Rails mailing list >Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >http://lists.rubyonrails.org/mailman/listinfo/rails > > >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails