【问题标题】:How to add a search capability in rails 3 app using the named scope?如何使用命名范围在 Rails 3 应用程序中添加搜索功能?
【发布时间】:2011-04-18 06:35:30
【问题描述】:

大家好。我想知道如何在 rails 3 中使用命名范围进行简单搜索。我已经在控制台中成功完成了它,但我找不到任何使用视图的示例。

这是模型trap.rb的代码:

class Trap < ActiveRecord::Base
 scope :by_date_entry, lambda { |arg| where(["traps.date_entry = ?",arg])}

 scope :by_empcode, lambda { |arg| where(["traps.empcode = ?",arg])}
end

在控制器中traps_controller.rb

class TrapsController < ApplicationController
 # GET /traps
 # GET /traps.xml
 def index
  @traps = Trap.all

  respond_to do |format|
   format.html # index.html.erb
   format.xml  { render :xml => @traps }
  end
 end

 # GET /traps/1
 # GET /traps/1.xml
 def show
  @trap = Trap.find(params[:id])

  respond_to do |format|
   format.html # show.html.erb
   format.xml  { render :xml => @trap }
  end
 end

 # GET /traps/new
 # GET /traps/new.xml
 def new
  @trap = Trap.new

  respond_to do |format|
   format.html # new.html.erb
   format.xml  { render :xml => @trap }
  end
 end

 # GET /traps/1/edit
 def edit
  @trap = Trap.find(params[:id])
 end

 # POST /traps
 # POST /traps.xml
 def create
  @trap = Trap.new(params[:trap])

  respond_to do |format|
   if @trap.save
    format.html { redirect_to(@trap, :notice => 'Trap was successfully created.') }
    format.xml  { render :xml => @trap, :status => :created, :location => @trap }
   else
    format.html { render :action => "new" }
    format.xml  { render :xml => @trap.errors, :status => :unprocessable_entity }
   end
  end
 end

 # PUT /traps/1
 # PUT /traps/1.xml
 def update
  @trap = Trap.find(params[:id])

  respond_to do |format|
   if @trap.update_attributes(params[:trap])
    format.html { redirect_to(@trap, :notice => 'Trap was successfully updated.') }
    format.xml  { head :ok }
   else
    format.html { render :action => "edit" }
    format.xml  { render :xml => @trap.errors, :status => :unprocessable_entity }
   end
  end
 end

 # DELETE /traps/1
 # DELETE /traps/1.xml
 def destroy
  @trap = Trap.find(params[:id])
  @trap.destroy

  respond_to do |format|
   format.html { redirect_to(traps_url) }
   format.xml  { head :ok }
  end
 end
end

在我看来index.html.erb

<h2>Time Reconciliation Application for Payroll 1.0</h2>

<table>
 <tr>
  <th>Empcode</th>
  <th>Date entry</th>
  <th></th>
  <th></th>
  <th></th>
 </tr>

 <% @traps.each do |trap| %>
 <tr>
  <td><%= trap.empcode %></td>
  <td><%= trap.date_entry %></td>
  <td><%= link_to 'Show', trap %></td>
  <td><%= link_to 'Edit', edit_trap_path(trap) %></td>
  <td><%= link_to 'Destroy', trap, :confirm => 'Are you sure?', :method => :delete %></td>
 </tr>
 <% end %>
</table>

<br />

<%= link_to 'New Trap', new_trap_path %>

我想放一个搜索文本框,用户可以在其中输入index.html.erb 中的employee_code 或date_entry。我使用 meta_search 和 meta_where 做了类似的事情,但我更喜欢使用 named_scope 但我不知道如何使用视图和控制器来显示它。我的模型 trap.rb 中的代码正在我的控制台上运行。我只是不知道如何让它出现在视图中。请帮忙...

【问题讨论】:

    标签: ruby-on-rails-3 activerecord scope named-scope


    【解决方案1】:

    我可能会在控制器中做这样的事情:

    def index
      # the scoped method returns a prepared database call with 
      # no arguments so the database is not called yet.
      @traps = Trap.scoped
    
      # if any parameter to filter is supplied then use the scope
      @traps = @traps.by_date_entry(params[:date_entry]) if params[:date_entry]
      @traps = @traps.by_empcode(params[:empcode]) if params[:empcode]
    end
    

    然后在视图中,您可以使用表单来发送参数,或者如果您只是想尝试一下,然后为特定日期或 empcode 创建一个链接,如下所示:

    <td><%= link_to trap.date_entry, traps_path(:date_entry => trap.date_entry) %></td>
    

    因此,当您单击链接时,它将发送 date_entry 以供您的范围使用。顺便说一句,traps_path 当然只有在您指定资源时才有效:在您的 routes.rb 中指定的陷阱

    【讨论】:

    • 我会做类似 form_for 的事情吗?
    • 我不知道,我想这取决于你。如果你想有一个搜索字段,用户可以手动编写数据进行搜索,那么我建议只使用 form_tag 并使用 search_field_tag。
    【解决方案2】:

    我还发现这个 Railscast 对这个主题很有帮助:

    http://railscasts.com/episodes/215-advanced-queries-in-rails-3

    【讨论】:

      猜你喜欢
      • 2011-11-11
      • 2014-11-18
      • 1970-01-01
      • 2016-09-11
      • 2010-10-24
      • 1970-01-01
      • 2021-07-26
      • 2017-09-02
      • 2020-02-04
      相关资源
      最近更新 更多