search post - maxivak/simple_search_filter GitHub Wiki
Search using POST-and-Redirect pattern and save data in session
Workflow:
- Data from search form is POSTed to server
- The search data is stored in session
- It redirects to the page to show data
If the page is opened without params then it will restore saved filter from session.
Options
Options for search_filter:
- save_session - set to true,
- search_method - set to :post_and_redirect,
- url - route for index page
- search_url - route for search page
- search_action - name of the action in controller corresponding to :search_url. By default, :search_action = :search
Example:
search_filter :index, {save_session: true, search_method: :post_and_redirect, url: :products_url, search_url: :search_products_url, search_action: :search} do
Workflow
- User submits the filter form;
- Form data are POSTed to page defined by
:search_url
and processed by controller action defined by:search_action
; - Data for each filter are saved in session;
- It redirects to the page defined by :url.
Routes
if used post method to a separate action (search_method: :post_and_redirect) then a route for search action should be created:
Define route for processiong POST request:
# config/routes.rb
resources :products do
collection do
post 'search'
end
end
Example:
class ProductsController < ApplicationController
search_filter :index, {save_session: true, search_method: :post_and_redirect, url: :products_url, search_url: :search_products_url, search_action: :search} do
default_order "price", 'asc'
field :title, :string, :text, {label: 'Title', default_value: '', condition: :like_full}
end
def index
@items = Product.by_filter (@filter)
end
end
Route:
# config/routes.rb
resources :products do
collection do
post 'search'
end
end