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 8088863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:13:01+00:00 2026-06-05T19:13:01+00:00

I’m implementing a search before data entry, to make sure the user is not

  • 0

I’m implementing a search before data entry, to make sure the user is not duplicating an existing entry, and I am having trouble identifying how to capture and use the number of search results to direct the user to the next step.

I am using rails 3 and the pg_search gem

models/request.rb

class Request < ActiveRecord::Base
  attr_accessible :description, :title, :user_id, :who

  belongs_to :user
  has_many :comments

  include PgSearch
  pg_search_scope :search, against: [:title, :description, :who],
     using: {tsearch: {dictionary: "english"}},
       associated_against: {user: :name},
       ignoring: :accents

  def self.request_search(query)
    if query.present?
      search(query)
      where("title @@ :q or description @@ :q or who @@ :q", q: query)
    else
      scoped
    end
  end
end

requests_controller.rb

  def index
    @requests = Request.request_search(params[:search]).order(sort_column + " " +  sort_direction).paginate(:per_page => 4, :page => params[:page])
    @users = User.all
  end

— begin edit (per Jesse’s answer)

requests_controller.rb (cont.)

  private

  def sort_column
    Request.column_names.include?(params[:sort]) ? params[:sort] : "title"
  end

  def sort_direction
    %w[asc desc].include?(params[:direction]) ? params[:direction] : "asc"
  end

application_helper.rb

  def sortable(column, title = nil)
    title ||= column.titleize
    css_class = column == sort_column ? "current #{sort_direction}" : nil
    direction = column == sort_column && sort_direction == "asc" ? "desc" : "asc"
    link_to title, params.merge(:sort => column, :direction => direction, :page => nil), {:class => css_class}
  end

_requests.html.erb (partial)

<%= hidden_field_tag :direction, params[:direction] %>
<%= hidden_field_tag :sort, params[:sort] %>

<div class="row">
    <table class="table table-striped pretty">
       <thead>
          <tr>
            <th><%= sortable "title" %></th>
            <th><%= sortable "who", "Requested Pro" %></th>
            <th><%= sortable "created_at", "When" %></th>
            <th><%= sortable "request.user", "Requested by" %></th>
          </tr>
       </thead>
       <tbody>
       <% @requests.each do |request| %>
         <tr>
           <td><%= request.title %></td>
           <td><%= request.who %></td>
           <td><%= request.created_at %></td>
           <td><%= request.user.name %></td>
           <td><%= link_to 'Show', request, :class => 'btn btn-mini'%></td>
           <% if can? :update, @course %>
           <td><%= link_to 'Edit', edit_request_path(request) %></td>
               <td><%= link_to 'Destroy', request, 
                            confirm: 'Are you sure?', 
                            method: :delete, 
                            :class => 'btn btn-mini' %></td>
           <% end %>
         </tr>
      <% end %>
      </tbody>
    </table>
    <div class="pull-right"><%= will_paginate @requests %></div>    
</div>
<%= button_to 'New Request', new_request_path, :class => 'btn btn-large btn-primary mleft20 mtop20' %>

— end edit

For example, there are two ways to arrive at the index page from other pages, after a search or through a link. If the user arrives at the index after a search, I would like to insert some text before the search results in the view that says: Your search yielded “x” items. If you don’t see what you’re looking for, enter a new request by clicking on the button.

Else, if the user just linked to the index, I want to be able to hide the text and just display the index as is.

I am thinking I can access the search attributes (if it is right to call them that) through request_search somehow, but I’m not sure how to implement it in the view (after several attempts).

Secondly, if I can add, I am looking to copy the search text into one of the entry fields if the user decides to create a new entry…

Please let me know if I have stated the question(s) clearly and if you can shed some light for me. Thanks in advance.

  • 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-06-05T19:13:04+00:00Added an answer on June 5, 2026 at 7:13 pm
      def index
        @requests = Request.request_search(params[:search]).order(sort_column + " " +  sort_direction).paginate(:per_page => 4, :page => params[:page])
        @users = User.all
        @search = params[:search] 
     end
    

    in your view:

    <% if @search.present? %> 
      <p>Your search yielded <%= pluralize(@requests.total_entries, "item") %>. If you don't see what you're looking for, enter a new request by clicking on the button.</p>
    <% end %>
    
    <%= form_tag, url: "/" do %>
      <%= label_tag :search, "Search for:"%>
      <%= text_field_tag :search, value: @search %>
      <%= submit_tag "Search" %>
    <% end %>
    

    Final note: Looks like you must have left out the code that gets “sort_column”. As is, you’ll have trouble running the above code since “sort_column” will be an unreferenced variable.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I need to clean up various Word 'smart' characters in user input, including but
I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I have some data like this: 1 2 3 4 5 9 2 6

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.