Guest User

Untitled

a guest
Feb 19th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. class ItemController < ApplicationController
  2. #Our controller only defines one action, called list. This action will have to handle every request sent to our application.
  3. def list
  4.  
  5. items_per_page = 10
  6. #The reverse string in the sort parameter indicates that sorting should be made in descending order.
  7. sort = case params['sort']
  8. when "name" then "name"
  9. when "qty" then "quantity"
  10. when "price" then "price"
  11. when "name_reverse" then "name DESC"
  12. when "qty_reverse" then "quantity DESC"
  13. when "price_reverse" then "price DESC"
  14. end
  15. #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.
  16. conditions = ["name LIKE ? or price LIKE ?"] + ["%#{params[:query]}%"] * 2
  17.  
  18. #After that, we assign the total number of items in our database matching the conditions to the @total variable.
  19. @total = Item.count(:conditions => conditions)
  20. @items_pages, @items = paginate :items, :order => sort, :conditions => conditions, :per_page => items_per_page
  21.  
  22. if request.xml_http_request?
  23. render :partial => "items_list", :layout => false
  24. end
  25.  
  26. end
  27.  
  28. end
Add Comment
Please, Sign In to add comment