Reputation: 14509
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.
Upvotes: 2
Views: 161
Reputation: 40277
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.
Upvotes: 1