I''m having a problem with creating simple search form. Let''s say I''ve got table COMPANIES and on my list view I''d like to add one-field-search form. My search method allways gives me some errors. Can''t move ahead... grrr Don''t know how to catch search_string and then use it in my controller :: search method.. yes, I''m newbie... thanks milos kelemen
milos kelemen wrote:> I''m having a problem with creating simple search form. Let''s say I''ve > got table COMPANIES and on my list view I''d like to add one-field-search > form. My search method allways gives me some errors. Can''t move ahead... > grrr > > Don''t know how to catch search_string and then use it in my controller > :: search method.. > > yes, I''m newbie... >Maybe you could post the code you are working on? Or at least the errors you are seeing? The basic process should be to create a form with a searhc text field and submit button in it. This should call an action on the specified controller which does the searching and returns the results in an Array or similar. Chris
Chris Roos wrote:> milos kelemen wrote: > >> I''m having a problem with creating simple search form. Let''s say I''ve >> got table COMPANIES and on my list view I''d like to add >> one-field-search form. My search method allways gives me some errors. >> Can''t move ahead... grrr >> >> Don''t know how to catch search_string and then use it in my controller >> :: search method.. >> >> yes, I''m newbie... >> > Maybe you could post the code you are working on? Or at least the > errors you are seeing? > > The basic process should be to create a form with a searhc text field > and submit button in it. This should call an action on the specified > controller which does the searching and returns the results in an Array > or similar. > > ChrisI''ve already figered it out, however, I''m breaking another difficulty: my search form is: _search.rhtml <%= start_form_tag :action => ''search'' %> <%= text_field "contact", "str", :size => "10", :maxlenght => "5" %> <%= submit_tag "search contact" %> <%= end_form_tag %> ################## my ContactsController #just search method def search if request.post? @search_string = params["contact"]["str"] if @search_string == "" @flash[''empty-notice''] = "Your search string is empty" else @contacts = Contact.find(:all, :conditions =>"name LIKE ''%#{@search_string}%'' or surname LIKE ''%#{@search_string}%''") render :action => "search" end end end ##################### in @contacts I get what I want, however I don''t know how to paginate this var. any thoughts??
milos kelemen wrote:> Chris Roos wrote: > >> milos kelemen wrote: >> >>> I''m having a problem with creating simple search form. Let''s say I''ve >>> got table COMPANIES and on my list view I''d like to add >>> one-field-search form. My search method allways gives me some errors. >>> Can''t move ahead... grrr >>> >>> Don''t know how to catch search_string and then use it in my >>> controller :: search method.. >>> >>> yes, I''m newbie... >>> >> Maybe you could post the code you are working on? Or at least the >> errors you are seeing? >> >> The basic process should be to create a form with a searhc text field >> and submit button in it. This should call an action on the specified >> controller which does the searching and returns the results in an >> Array or similar. >> >> Chris > > > I''ve already figered it out, however, I''m breaking another difficulty: > > my search form is: > _search.rhtml > <%= start_form_tag :action => ''search'' %> > <%= text_field "contact", "str", :size => "10", :maxlenght => "5" %> > <%= submit_tag "search contact" %> > <%= end_form_tag %> > ################## > > my ContactsController > > #just search method > def search > if request.post? > @search_string = params["contact"]["str"] > if @search_string == "" > @flash[''empty-notice''] = "Your search string is empty" > else > @contacts = Contact.find(:all, :conditions =>"name LIKE > ''%#{@search_string}%'' or surname LIKE ''%#{@search_string}%''") > > render :action => "search" > end > end > end > > ##################### > in @contacts I get what I want, however I don''t know how to paginate > this var. > > any thoughts??:) @contact_pages, @contacts = paginate :contact, :per_page => 3, :conditions =>"name LIKE ''%#{@search_string}%'' or surname LIKE ''%#{@search_string}%''" am I right..?
On Fri, 26 Aug 2005, milos kelemen wrote:> I''ve already figered it out, however, I''m breaking another difficulty: > > my search form is: > _search.rhtml > <%= start_form_tag :action => ''search'' %> > <%= text_field "contact", "str", :size => "10", :maxlenght => "5" %>^^ s/lenght/length/ if you pasted this... Hugh
milos kelemen wrote:> milos kelemen wrote: > >> Chris Roos wrote: >> >>> milos kelemen wrote: >>> >>>> I''m having a problem with creating simple search form. Let''s say >>>> I''ve got table COMPANIES and on my list view I''d like to add >>>> one-field-search form. My search method allways gives me some >>>> errors. Can''t move ahead... grrr >>>> >>>> Don''t know how to catch search_string and then use it in my >>>> controller :: search method.. >>>> >>>> yes, I''m newbie... >>>> >>> Maybe you could post the code you are working on? Or at least the >>> errors you are seeing? >>> >>> The basic process should be to create a form with a searhc text field >>> and submit button in it. This should call an action on the specified >>> controller which does the searching and returns the results in an >>> Array or similar. >>> >>> Chris >> >> >> >> I''ve already figered it out, however, I''m breaking another difficulty: >> >> my search form is: >> _search.rhtml >> <%= start_form_tag :action => ''search'' %> >> <%= text_field "contact", "str", :size => "10", :maxlenght => "5" %> >> <%= submit_tag "search contact" %> >> <%= end_form_tag %> >> ################## >> >> my ContactsController >> >> #just search method >> def search >> if request.post? >> @search_string = params["contact"]["str"] >> if @search_string == "" >> @flash[''empty-notice''] = "Your search string is >> empty" else >> @contacts = Contact.find(:all, :conditions =>"name LIKE >> ''%#{@search_string}%'' or surname LIKE ''%#{@search_string}%''") >> render :action => "search" >> end >> end >> end >> >> ##################### >> in @contacts I get what I want, however I don''t know how to paginate >> this var. >> >> any thoughts?? > > > > :) > > @contact_pages, @contacts = paginate :contact, :per_page => 3, > :conditions =>"name LIKE ''%#{@search_string}%'' or surname LIKE > ''%#{@search_string}%''" > > am I right..? >It looks good, the only thing I''d suggest is protecting yourself against sql injection attacks by not placing the @search_string directly into the sql query string. Instead, use either of the below. ["name like ? or surname like ?", @search_string, @search_string] ["name like :search or surname like :search", {:search => @search_string}] Using either of these two approaches will mean that AR ''sanitizes'' the input to the DB. Chris