Ruby on Rails:搜索表单 - 多个搜索字段 [英] Ruby on Rails: Search Form - multiple search fields

查看:162
本文介绍了Ruby on Rails:搜索表单 - 多个搜索字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个允许用户搜索数据库的应用程序。搜索页面布局会显示一些下拉菜单,它们会显示数据库中已有数据以缩小搜索范围,还有文本框以允许用户输入关键词,如项目名称。我遇到了一个问题,让rails会将所有已输入的信息都放在搜索表单中,然后执行一个大搜索。



这是我的搜索布局的一部分:

 <%= form_tag search_path,:method => 'get'do%> 

<%= hidden_​​field_tag:方向,参数[:方向]%>
<%= hidden_​​field_tag:sort,params [:sort]%>
< p>
<%= text_field_tag:search,params [:search]%>
<%= submit_tag搜索项目名称,:project_name =>无%>
< / p>
< p>
<%= text_field_tag:search,params [:search]%>
<%= submit_tag搜索客户端,:客户端=>无%>
< / p>
<%end%>

这是我在项目控制器中的索引和搜索操作:

  def索引
@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

这里是我的模型/ project.rb文件的一部分

  def self.search(search)
if search
where('project_name LIKE?',%#{search}%)|| where'('client LIKE?','%#{search}%)
else
scoped
end
end

正如您所看到的,我只是试图搜索project_name或客户端。如果我能得到这个工作,我会将其扩展到其他领域。



目前的功能是,当我试图搜索它的两个盒子时,它覆盖一个,只做一个字段搜索。



我是ROR的全新人物,所以希望有人可以提供帮助。任何建议将不胜感激。



预先感谢!

解决方案


此问题已在另一个问题中得到解决: Ruby on Rails:高级搜索


基于您的问题,@Chris Wise假设您在项目表中有两列: project_name 客户端

我可能是错的,但我认为你实际上有两个模型客户端 Project ,其中一个客户有很多项目。如果是这样,您需要在每个模型中定义搜索:

客户端模型:

  def self.search search_term 
return scoped除非search_term.present?
其中(['client_name LIKE?','%#{search_term}%])#client_name表示列名,将其更改为正确的名称。
end

项目模式:

  def self.search search_term 
return scoped除非search_term.present?
其中(['project_name LIKE?','%#{search_term}%])#project_name表示列名称,将其更改为正确的名称。
end

您的表单:

 <%= form_tag projects_path,方法:: get do%> 
<%= text_field_tag:project_name,params [:project_name]%>
<%= text_field_tag:client,params [:client]%>
<%= submit_tag搜索,名称:nil%>
<%end%>

然后你的控制器:

 #返回符合搜索条件的所有项目
@project_search = Project.search(params [:project_name])。all
#返回符合搜索条件的所有客户端
@clients_search = Client.search(params [:client])。all

我希望它有帮助...

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!

解决方案

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...

这篇关于Ruby on Rails:搜索表单 - 多个搜索字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆