Reputation: 155
For my search form, I'm using the solution here: https://stackoverflow.com/a/12622896/1943735
My search page looks like:
= simple_form_for search_query, :url => :search, do |f|
= f.input :first_name
= f.input :last_name
...
Table To Display Results is here
In my controller, I have this:
def search
search_params = params[:search_query]
@people = Result of some searching
end
The above code works, except that the user can't see what values they just typed in to the form and get only the results. The form renders again, and is now blank.
My attempt at the solution is:
def search
search_params = params[:search_query]
@people = Result of some searching
@search_query = params[:search_query]
end
However, now the form throws a NoMethodError
with undefined method 'first_name' for #<ActionController::Parameters:0x007fa5688aa268>
at the line = f.input :first_name
How can I pass my search_query back to the form?
Upvotes: 0
Views: 270
Reputation: 15917
I think the trick here is to use a GET instead of a POST, that way those search parameters will remain visible in your view.
Here's an example straight out of a working app I wrote...
def index
authorize! :index, Employee
params[:filter] ||= 'All' # Default to All Employees
params[:search] ||= '' # Default to blank
page_title ||= params[:filter].titleize
# Clear the search field if a filter option was selected
if session[:employee_filter] != params[:filter]
session[:employee_filter] = params[:filter]
params[:search] = ''
end
# Generate an array of our filter elements (include all departments)
@page_links = %w[All Featured Admins]
Department.all.each do |department|
@page_links << [department.title, department.name]
end
# Check and execute the filter option (defaults to All above)
case params[:filter]
when 'All'
@employees = Employee.search(params[:search]).by_name.page(params[:page]).per(15)
when 'Featured'
# We need this to be all on one page so that we can drag-and-drop position employees
@employees = Employee.search(params[:search]).is_featured.page(params[:page]).per(99999)
when 'Admins'
@employees = Employee.search(params[:search]).admins.by_name.page(params[:page]).per(15)
@page_title = 'Site Administrators'
else
@employees = Employee.search(params[:search]).dept(params[:filter]).by_index.page(params[:page]).per(15)
end
@page_title ||= "#{page_title} Employees"
@page_title = "'#{params[:search]}' in #{page_title}" unless params[:search].blank?
end
And here's the search method in the Employee model:
def self.search(search)
if search
results = where('last_name ILIKE ? OR first_name ILIKE ? OR email ILIKE ?', "%#{search}%", "%#{search}%", "%#{search}%")
results = where('title ILIKE ?', "%#{search}%") if results.blank?
results = joins(:location).where('locations.name ILIKE ?', "%#{search}%") if results.blank?
return results
end
end
And, finally, the search form from the index (in HAML format):
.sort-index
= form_tag admin_employees_path, method: 'get' do
= select_tag :filter, options_for_select(@page_links, params[:filter]), onchange: "this.form.submit();"
%br
= text_field_tag :search, params[:search], placeholder: 'Search Name, Title, or Location', size: 35
= submit_tag :search, name: nil, hidden: true
It's got some extra search stuff in there (such as additional filters), so let me know of that complicates it too much for you.
Upvotes: 1