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
endchildren.rb
class Child
include Mongoid::Document
field :hobby, type: String
field :name, type: String
field :born, type: Date
embedded_in :parent
endparents_controller.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家长的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 %>我想实现一个搜索功能,不仅可以在主文档中搜索,还可以在嵌套文档中搜索。不仅可以在文本字段中搜索,还可以使用日期范围进行搜索。有没有人能给我一些关于这方面的资源(教程)?我找不到任何像Ryan的简单搜索铁路广播的东西。如果有人能告诉我如何修改我的控制器和index.html.erb文件,我将非常感激。
在github https://github.com/mongoid/mongoid/blob/master/lib/mongoid/contextual/text_search.rb中还有一个与搜索相关的Durran页面。但坦率地说,这并没有给出任何线索来解决我的问题。
发布于 2013-07-01 18:21:58
感谢@Pierre-Louis Gottfrois的建议,帮助我做了一些进一步的研究。
我可以搜索两个字段(名字和爱好),如下所示。
我已经在产品的index.html.erb上添加了:
<%= form_tag parents_path, :method => 'get' do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>然后在parent.rb模型中,考虑到@Pierre-Louis的建议,我进行了更改:
def self.search(search)
if search
any_of({name: search}, {hobby: search})
end
endproducts_controller.rb已更改:
def index
if params[:search].empty?
@parents = Parent.all
else
@parents = Parent.search(params[:search])
end
end有3个问题仍然存在,我认为最好把它们分开放在不同的帖子里:
在搜索时如何忽略区分大小写
,则通过指定
documents
发布于 2013-07-01 15:17:37
例如,您需要让您的index方法接受一个search参数
def index
@parents = if params[:search]
Parent.where(name: params[:search])
else
Parent.all
end
end这是基本的想法。您可以考虑使用类方法来执行更复杂的搜索(例如,使用born属性)。
def index
if params[:search]
@parents = Parent.search_for(params[:search])
else
@parents = Parent.all
end
end模型中的:
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]', '' %>https://stackoverflow.com/questions/17398326
复制相似问题