2013-06-27 129 views
1

我必須在網站的主頁上修改搜索。代碼用Ruby語言編寫,並使用「Rails」框架工作。Ruby on Rails多個搜索查詢

***圖片******

enter image description here

這裏,已經是與郵件ID搜索的選項。但也有一個id字段。需要實施多重搜索。

「視圖」(MVC架構)已經編寫的代碼: -

<form action="https://stackoverflow.com/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> 

的 「控制器」 已經編寫的代碼(MVC架構): -

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 
  • 請幫助我到在控制器文件中使用條件爲的多重搜索滿足以下條件: -

  • 搜索與ID

  • 搜索與電子郵件(已寫入)
  • 搜索與電子郵件和ID

回答

2

在查看部分

<form action="https://stackoverflow.com/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> 

在搜索行動

@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('-', ' ') 
      ) 

它會搜索記錄與身份證,電子郵件和身份證或電子郵件。

+2

謝謝Bachan Smruty :) – Monisha