Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4544152
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T15:37:55+00:00 2026-05-21T15:37:55+00:00

HI Guys. I wanna know how can I make a simple search using named

  • 0

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…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T15:37:56+00:00Added an answer on May 21, 2026 at 3:37 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi guys i am going to make a android game. i wanna know that
Guys I m using a simple listview. I have two activities one main and
Can I check with you guys if I wanna create an app that can
Hey guys, I have some questions would like to ask: I wanna ask how
Hi guys , I'm now working with ActiveMQ and wanna know what is the
Hey guys I have a question. I wanna create a profile page for each
Guys i have a html string like below; <html> <head><title></title></head> <body>i wanna remove <span
Hey guys, I wanna ask if there is an IDE to which i can
Hey guys i'm using this plugin from Hawkee. Its like twitter where you can
Guys, I've came across this problem I can't resolve myselg, I'm pretty sure I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.