2013-06-27 132 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