如何使用命名范围在rails 3应用程序中添加搜索功能?

发布于 2024-11-02 04:01:27 字数 3239 浏览 2 评论 0原文

嗨,大家好。我想知道如何使用 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中输入employee_code或date_entry .erb。我已经使用meta_search和meta_where做了类似的事情,但我更喜欢使用named_scope,但我不知道如何使用视图和控制器显示它。我的模型 trap.rb 中的代码正在我的控制台上运行。我只是不知道如何让它出现在视图中。请帮忙...

HI Guys. I wanna know how can I make a simple search using named scope in rails 3. I have successfully done it in the console but I can't find any example that uses the views.

Here is the code for the model 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

In the controller 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

And in my view 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 %>

I wanna put like a search text box in which the user can input the employee_code or the date_entry in the index.html.erb. I have done something like this using meta_search and meta_where but I would prefer to use the named_scope but I don't know how to display this using view and controller. The codes in my model trap.rb is working on my console. I just don't know how to make it appear in the view. Pls help...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半步萧音过轻尘 2024-11-09 04:01:27

我可能会在控制器中执行类似的操作:

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 中指定了 resources :traps 时才有效

I would probably do something like this in the controller:

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

And then in the view, you could either go with a form to send the parameters, or if you just want to try this then create a link for the specific dates or empcodes like this:

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

So that when you click the link it will send the date_entry to be used by your scope. By the way, the traps_path is of course only valid as long as you specified resources :traps in your routes.rb

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文