Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8302491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:18:19+00:00 2026-06-08T17:18:19+00:00

I am trying to create an application that allows the user to search a

  • 0

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 brand new to ROR so hopefully someone can help. Any suggestions will be appreciated.

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T17:18:21+00:00Added an answer on June 8, 2026 at 5:18 pm

    This problem has been solved in another question: Ruby on Rails: Advanced search

    Based on your question, @Chris Wise is assuming that you have two columns in your projects table: project_name and client.

    I might be wrong, but I think you actually have two models Client and Project, where a client has many projects. If so, you need to define the search in each model:

    Client model:

    def self.search search_term
      return scoped unless search_term.present? 
      where(['client_name LIKE ?', "%#{search_term}%"]) #client_name means the column name, change it to the correct name.
    end
    

    Project model:

    def self.search search_term
      return scoped unless search_term.present? 
      where(['project_name LIKE ?', "%#{search_term}%"]) #project_name means the column name, change it to the correct name.
    end
    

    Your form:

    <%= form_tag projects_path, method: :get do %>
      <%= text_field_tag :project_name, params[:project_name] %>
      <%= text_field_tag :client, params[:client] %>
      <%= submit_tag "Search", name: nil %>
    <% end %>
    

    Then your controller:

    #return all projects that match the search criteria
    @project_search = Project.search(params[:project_name]).all
    #return all clients that match the search criteria
    @clients_search = Client.search(params[:client]).all
    

    I hope it helps…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently trying to make an application that allows the user to create
I am trying create a small web application that allows a user to login
I'm trying to create an app that allows the user to open the android
I'm building an application that allows the user to create a Human model (graphical
I'm trying to create a web application that will allow a user to post
I'm trying to put in place a Flex application that allows a user to
I'm creating an air application that allows the user to create, edit and save
I'm trying to write an application that will allow a non-administrator user to create
I'm trying to create a PHP based application that allows students from my school
So I'm writing an application that would allow a user to create a type

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.