1

I have to modify a search in the home page of a site. The code is written in Ruby language and used "Rails" frame work.

***Image******

enter image description here

Here, there is already an option for search with mail id. But there is an id field too. Need to implement multiple search.

Already written code of "view" (MVC architecture):-

 <form action="/users/search" method="POST">
   <label for="email">Email</label>
   <input type="text" name="email" />&nbsp;
   <input type="submit" name="search" value="Search" />
   <input type="hidden" name="authenticity_token" value="<%= form_authenticity_token %>" />
 </form>

Already written code of its "controller" (MVC architecture):-

 def search 
    @users = User.paginate(
            :conditions => ["email LIKE ?", '%' + params[:email] + '%'], 
            :page => params[:page], :per_page => 25, 
            :order => params[:order].nil? ? 
            :email :  params[:order].gsub('-', ' ')
            )

   if @users.length == 1
     redirect_to edit_user_path(@users.first)
   else
     render "index"
   end
  end
  • Please help me to use multiple search with the condition in controller file satisfying these:-

  • Search with id

  • Search with email (already written)
  • Search with email and id

1 Answer 1

2

In the view section

<form action="/users/search" method="POST">
       <label for="email">Email</label>
       <input type="text" name="email" />&nbsp;
       <label for="user_id">ID</label>
       <input type="text" name="user_id" />&nbsp;
       <input type="submit" name="search" value="Search" />
       <input type="hidden" name="authenticity_token" value="<%= form_authenticity_token %>" />
</form>

In the search action

@users = User.paginate(
            :conditions => ["email LIKE ? or id = ?", '%' + params[:email] + '%', params[:user_id]], 
            :page => params[:page], :per_page => 25, 
            :order => params[:order].nil? ? 
            :email :  params[:order].gsub('-', ' ')
            )

it will search the records with id, email and id or email.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.