Created
December 3, 2014 10:26
-
-
Save launchkit-codes/2720aef820a818eb9ad0 to your computer and use it in GitHub Desktop.
Sort by ActiveRecord column
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <%= link_to "Name", sort: "Name" %> | |
| <%= link_to "Price", sort: "price" %> | |
| <%= link_to "Popular", sort: "popular" %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Model | |
| class Item < ActiveRecord::Base | |
| scope :sort_by, ->(attr) { order(attr) } | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ItemsController < ApplicationController | |
| def index | |
| @items = Item.sort_by(params[:sort]) | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment