I'm new to RoR so please excuse a simple question. I've finished the first part of the Depot Application from AWDwR. When I create a new product it goes to the bottom of the list. How do I get new products to sort to the top? Thanks in advance.
Hi
DP777 wrote:
I'm new to RoR so please excuse a simple question. I've finished the first part of the Depot Application from AWDwR. When I create a new product it goes to the bottom of the list. How do I get new products to sort to the top?
Find the find() method in the relevant controller and add ":order => 'id DESC'" to the parameters. This will find return the products from the database with the most recent first.
If your product list url is site.com/products/list, you should have a file called app/controllers/products_controller.rb with a method similar to this:
def list @products = Product.find(:all) [..] end
change this to:
def list @products = Product.find(:all, :order => 'id DESC') [..] end
Your file and method names may differ but this should give you a rough idea.
Cheers,
Mark
Thats all right, but if you use this book, you have following code in app\controllers\admin_controller.rb: ... def list @product_pages, @products = paginate :products, :per_page => 10 end ...
if you want to sort products by id, you should rewrite this code us:
def list @product_pages, @products = paginate :products, :per_page => 10, :order => 'id DESC' end ...
Try it, or may be you want sort by title or price?