Search 在Rails 4上使用Mongoid搜索,不仅搜索字符串字段,还搜索日期范围
parent.rbSearch 在Rails 4上使用Mongoid搜索,不仅搜索字符串字段,还搜索日期范围,search,mongoid,ruby-on-rails-4,Search,Mongoid,Ruby On Rails 4,parent.rb class Parent include Mongoid::Document field :name, type: String field :hobby, type: String field :born, type: Date embeds_many :children accepts_nested_attributes_for :children end children.rb class Child include Mongoid::D
class Parent
include Mongoid::Document
field :name, type: String
field :hobby, type: String
field :born, type: Date
embeds_many :children
accepts_nested_attributes_for :children
end
children.rb
class Child
include Mongoid::Document
field :hobby, type: String
field :name, type: String
field :born, type: Date
embedded_in :parent
end
家长控制器.rb
class ParentsController < ApplicationController
before_action :set_parent, only: [:show, :edit, :update, :destroy]
# GET /parents
# GET /parents.json
def index
@parents = Parent.all
end
# GET /parents/1
# GET /parents/1.json
def show
end
# GET /parents/new
def new
@parent = Parent.new
end
# GET /parents/1/edit
def edit
end
# POST /parents
# POST /parents.json
def create
@parent = Parent.new(parent_params)
respond_to do |format|
if @parent.save
format.html { redirect_to @parent, notice: 'Parent was successfully created.' }
format.json { render action: 'show', status: :created, location: @parent }
else
format.html { render action: 'new' }
format.json { render json: @parent.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /parents/1
# PATCH/PUT /parents/1.json
def update
respond_to do |format|
if @parent.update_attributes(parent_params)
format.html { redirect_to @parent, notice: 'Parent was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @parent.errors, status: :unprocessable_entity }
end
end
end
# DELETE /parents/1
# DELETE /parents/1.json
def destroy
@parent.destroy
respond_to do |format|
format.html { redirect_to parents_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_parent
@parent = Parent.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def parent_params
params.require(:parent).permit(:name, :hobby, :born)
end
end
class ParentsController
家长的index.html.erb:
<h1>Listing parents</h1>
<table>
<thead>
<tr>
<th>Name</th>
<th>Born</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<% @parents.each do |parent| %>
<tr>
<td><%= parent.name %></td>
<td><%= parent.born %></td>
<td><%= link_to 'Show', parent %></td>
<td><%= link_to 'Edit', edit_parent_path(parent) %></td>
<td><%= link_to 'Destroy', parent, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Parent', new_parent_path %>
<%= form_tag parents_path, :method => 'get' do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
列出父母
名称
天生的
我想实现一个搜索功能,不仅在主文档中搜索,而且在嵌套文档中搜索。不仅在文本字段中搜索,而且在日期范围内搜索。有人能告诉我关于这方面的任何资源(教程)吗?我找不到像Ryan的简单搜索railscast这样的东西。
如果有人能告诉我如何修改控制器和index.html.erb文件,我将不胜感激
还有一个Durran的页面与github中的搜索相关。但坦率地说,这并没有给我任何解决问题的线索 例如,您希望您的
索引
方法采用搜索
参数
def index
@parents = if params[:search]
Parent.where(name: params[:search])
else
Parent.all
end
end
这是基本思想。您可以考虑使用类方法来进行更复杂的搜索(例如使用<代码>出生< /COD>属性)。
在您的模型中:
class Parent
def search_for(criterias)
# Logic regarding criterias selection should go here.
# Very basic idea bellow
Parent.where(name: criterias[:name], born: criterias[:born])
end
end
// First case
<%= text_field_tag :search, '' %>
// Second case
<%= text_field_tag 'search[name]', '' %>
<%= text_field_tag 'search[born]', '' %>
在您看来:
class Parent
def search_for(criterias)
# Logic regarding criterias selection should go here.
# Very basic idea bellow
Parent.where(name: criterias[:name], born: criterias[:born])
end
end
// First case
<%= text_field_tag :search, '' %>
// Second case
<%= text_field_tag 'search[name]', '' %>
<%= text_field_tag 'search[born]', '' %>
//第一种情况
//第二种情况
例如,您希望您的索引方法采用搜索
参数
def index
@parents = if params[:search]
Parent.where(name: params[:search])
else
Parent.all
end
end
这是基本思想。您可以考虑使用类方法来进行更复杂的搜索(例如使用<代码>出生< /COD>属性)。
在您的模型中:
class Parent
def search_for(criterias)
# Logic regarding criterias selection should go here.
# Very basic idea bellow
Parent.where(name: criterias[:name], born: criterias[:born])
end
end
// First case
<%= text_field_tag :search, '' %>
// Second case
<%= text_field_tag 'search[name]', '' %>
<%= text_field_tag 'search[born]', '' %>
在您看来:
class Parent
def search_for(criterias)
# Logic regarding criterias selection should go here.
# Very basic idea bellow
Parent.where(name: criterias[:name], born: criterias[:born])
end
end
// First case
<%= text_field_tag :search, '' %>
// Second case
<%= text_field_tag 'search[name]', '' %>
<%= text_field_tag 'search[born]', '' %>
//第一种情况
//第二种情况
感谢@Pierre-Louis Gottfrois的建议,这些建议帮助我做了进一步的研究
我可以搜索两个领域(姓名和爱好),如下所示
我在产品的index.html.erb中添加了:
<h1>Listing parents</h1>
<table>
<thead>
<tr>
<th>Name</th>
<th>Born</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<% @parents.each do |parent| %>
<tr>
<td><%= parent.name %></td>
<td><%= parent.born %></td>
<td><%= link_to 'Show', parent %></td>
<td><%= link_to 'Edit', edit_parent_path(parent) %></td>
<td><%= link_to 'Destroy', parent, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Parent', new_parent_path %>
<%= form_tag parents_path, :method => 'get' do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
产品\u controller.rb已更改:
def index
if params[:search].empty?
@parents = Parent.all
else
@parents = Parent.search(params[:search])
end
end
仍然存在3个问题,我认为最好将它们分在不同的帖子中:
如何在搜索时忽略区分大小写
如何搜索包含嵌套文档的文档
通过指定日期范围进行搜索的能力
如果任何单词包含搜索关键字,则将其视为结果
感谢@Pierre-Louis Gottfrois的建议,帮助我做了一些进一步的研究
我可以搜索两个领域(姓名和爱好),如下所示
我在产品的index.html.erb中添加了:
<h1>Listing parents</h1>
<table>
<thead>
<tr>
<th>Name</th>
<th>Born</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<% @parents.each do |parent| %>
<tr>
<td><%= parent.name %></td>
<td><%= parent.born %></td>
<td><%= link_to 'Show', parent %></td>
<td><%= link_to 'Edit', edit_parent_path(parent) %></td>
<td><%= link_to 'Destroy', parent, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Parent', new_parent_path %>
<%= form_tag parents_path, :method => 'get' do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
产品\u controller.rb已更改:
def index
if params[:search].empty?
@parents = Parent.all
else
@parents = Parent.search(params[:search])
end
end
仍然存在3个问题,我认为最好将它们分在不同的帖子中:
如何在搜索时忽略区分大小写
如何搜索包含嵌套文档的文档
通过指定日期范围进行搜索的能力
如果任何单词包含搜索关键字,则将其视为结果
我只尝试了:name字段。但它什么也没给我。甚至rails输出也没有显示任何活动。如果名字和bord字段只有一个搜索字段就好了。我还需要搜索日期范围。您是否有mongoid搜索的可用代码?我将在模型中再添加一个字符串类型的字段。在这种情况下,您可能希望在执行查询时使用$或运算符<代码>父项。({name:params[:search]},{born:params[:search]})中的任意一个
。如果要搜索范围,则需要多个搜索字段;)我只尝试了:name字段。但它什么也没给我。甚至rails输出也没有显示任何活动。如果名字和bord字段只有一个搜索字段就好了。我还需要搜索日期范围。您是否有mongoid搜索的可用代码?我将在模型中再添加一个字符串类型的字段。在这种情况下,您可能希望在执行查询时使用$或运算符<代码>父项。({name:params[:search]},{born:params[:search]})中的任意一个
。如果要搜索范围,则需要多个搜索字段;)在这里,我创建了一个新问题,不想在这里搞砸:)在这里,我创建了一个新问题,不想在这里搞砸:)