James McLaughlin
2012-Jul-20 14:34 UTC
Ruby on Rails: Search Form - multiple search fields
I am trying to create an application that allows the user to search a database. The search page layout would behave with some drop down menus that would show data already in the database to narrow the search, and also text boxes to allow the user to put in key words like the "project name". I''m having a problem getting rails to take all the info that has been entered in the search form, and performing one big search. Here is part of my search layout: <%= form_tag search_path, :method => ''get'' do %> <%= hidden_field_tag :direction, params[:direction] %> <%= hidden_field_tag :sort, params[:sort] %> <p> <%= text_field_tag :search, params[:search] %> <%= submit_tag "Search Project Name", :project_name => nil %> </p> <p> <%= text_field_tag :search, params[:search] %> <%= submit_tag "Search Client", :client => nil %> </p> <% end %> Here is my index and search actions in the project controller: def index @projects = Project.all respond_to do |format| format.html # index.html.erb format.json { render :json => @projects } end end def search @project_search = Project.search(params[:search]).order(sort_column + '' '' + sort_direction).paginate(:per_page => 5, :page => params[:page]) end and here is part of my models/project.rb file def self.search(search) if search where(''project_name LIKE ?'', "%#{search}%") || where(''client LIKE ?'', "%#{search}%") else scoped end end As you can see, I am just trying to search on either the project_name or the client. If I can get this working, I will be extending it onto other fields. The functionality at the moment is that, when I try to search it both boxes, it overwrites one, and only does one of the field searches. I am new to ROR so hopefully someone can help. Any suggestions will be appreciated. Thanks in advance! -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
On 20 July 2012 15:34, James McLaughlin <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> I am trying to create an application that allows the user to search a > database. The search page layout would behave with some drop down menus > that would show data already in the database to narrow the search, and > also text boxes to allow the user to put in key words like the "project > name". I''m having a problem getting rails to take all the info that has > been entered in the search form, and performing one big search. > > Here is part of my search layout: > > <%= form_tag search_path, :method => ''get'' do %> > > <%= hidden_field_tag :direction, params[:direction] %> > <%= hidden_field_tag :sort, params[:sort] %> > <p> > <%= text_field_tag :search, params[:search] %> > <%= submit_tag "Search Project Name", :project_name => nil %> > </p> > <p> > <%= text_field_tag :search, params[:search] %> > <%= submit_tag "Search Client", :client => nil %> > </p> > <% end %> > > Here is my index and search actions in the project controller: > > def index > @projects = Project.all > > respond_to do |format| > format.html # index.html.erb > format.json { render :json => @projects } > end > end > > def search > > @project_search = Project.search(params[:search]).order(sort_column > + '' '' + sort_direction).paginate(:per_page => 5, :page => params[:page]) > > > end > > and here is part of my models/project.rb file > > def self.search(search) > if search > where(''project_name LIKE ?'', "%#{search}%") || where(''client LIKE > ?'', "%#{search}%") > else > scoped > end > end > > As you can see, I am just trying to search on either the project_name or > the client. If I can get this working, I will be extending it onto other > fields. > > The functionality at the moment is that, when I try to search it both > boxes, it overwrites one, and only does one of the field searches. > > I am new to ROR so hopefully someone can help. Any suggestions > will be appreciated.I have not looked at your code in detail but if you have a look at the Rails Guide on Debugging you will find techniques that can help you debug your code. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.