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…
I would probably do something like this in the controller:
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:
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