# Ruby on Rails: Search Form - multiple search fields

**URL:** <https://discuss.rubyonrails.org/t/ruby-on-rails-search-form-multiple-search-fields/64551>\
**Category:** rubyonrails-talk\
**Created:** [July 20, 2012, 2:34pm UTC](https://discuss.rubyonrails.org/t/ruby-on-rails-search-form-multiple-search-fields/64551 "2012-07-20T14:34:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [July 20, 2012, 2:34pm UTC](https://discuss.rubyonrails.org/t/ruby-on-rails-search-form-multiple-search-fields/64551/1 "2012-07-20T14:34:48Z")

</div>

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:

&nbsp;&nbsp;&nbsp;&nbsp;\<%= form\_tag search\_path, :method =\> 'get' do %\>

&nbsp;&nbsp;\<%= hidden\_field\_tag :direction, params[:direction] %\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<%= hidden\_field\_tag :sort, params[:sort] %\> &nbsp;&nbsp;&nbsp;&nbsp;\<p\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= text\_field\_tag :search, params[:search] %\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= submit\_tag "Search Project Name", :project\_name =\> nil %\> &nbsp;&nbsp;&nbsp;&nbsp;\</p\> &nbsp;&nbsp;&nbsp;&nbsp;\<p\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= text\_field\_tag :search, params[:search] %\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= submit\_tag "Search Client", :client =\> nil %\> &nbsp;&nbsp;&nbsp;&nbsp;\</p\> &nbsp;&nbsp;&nbsp;&nbsp;\<% end %\>

Here is my index and search actions in the project controller:

&nbsp;&nbsp;&nbsp;&nbsp;def index &nbsp;&nbsp;@projects = Project.all

&nbsp;&nbsp;&nbsp;&nbsp;respond\_to do |format| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.html # index.html.erb &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.json { render :json =\> @projects } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;def search

&nbsp;&nbsp;&nbsp;&nbsp;@project\_search = Project.search(params[:search]).order(sort\_column + ' ' + sort\_direction).paginate(:per\_page =\> 5, :page =\> params[:page])

&nbsp;&nbsp;&nbsp;&nbsp;end

and here is part of my models/project.rb file

&nbsp;&nbsp;&nbsp;&nbsp;def self.search(search) &nbsp;&nbsp;&nbsp;&nbsp;if search &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;where('project\_name LIKE ?', "%#{search}%") || where('client LIKE ?', "%#{search}%") &nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scoped &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;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!

---

<div class="post-metadata">

**Author:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [July 20, 2012, 7:59pm UTC](https://discuss.rubyonrails.org/t/ruby-on-rails-search-form-multiple-search-fields/64551/2 "2012-07-20T19:59:27Z")

</div>

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
