Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ItemController < ApplicationController
- #Our controller only defines one action, called list. This action will have to handle every request sent to our application.
- def list
- items_per_page = 10
- #The reverse string in the sort parameter indicates that sorting should be made in descending order.
- sort = case params['sort']
- when "name" then "name"
- when "qty" then "quantity"
- when "price" then "price"
- when "name_reverse" then "name DESC"
- when "qty_reverse" then "quantity DESC"
- when "price_reverse" then "price DESC"
- end
- #Then, a conditions variable is constructed if a query request parameter is present. It is an SQL-like instruction which will be used to filter our database query results based on the content of the name field.
- conditions = ["name LIKE ? or price LIKE ?"] + ["%#{params[:query]}%"] * 2
- #After that, we assign the total number of items in our database matching the conditions to the @total variable.
- @total = Item.count(:conditions => conditions)
- @items_pages, @items = paginate :items, :order => sort, :conditions => conditions, :per_page => items_per_page
- if request.xml_http_request?
- render :partial => "items_list", :layout => false
- end
- end
- end
Add Comment
Please, Sign In to add comment