(Rails3.2.12)
プロジェクト作成
rails new sortable_table -T cd new sortable_table rails g scaffold Product name price:decimal released:date rake db:migrate
確認用初期データ投入
db/seeds.rb
Product.delete_all Product.create(name: "Katsuo", price: 500, released: 30.days.ago.to_date) Product.create(name: "Wakame", price: 300, released: 20.days.ago.to_date) Product.create(name: "Tarao", price: 50, released: 10.days.ago.to_date)
rake db:seed
コントローラ修正
app/view/controllers/products_controller.rb
def index @products = Product.order(params[:sort]) :
ビュー修正
app/view/products/index.html.erb
<h1>Isono-ke</h1> <table> <tr> <th> <%= link_to "Name", sort: "name" %> </th> <th> <%= link_to "Price", sort: "price" %> </th> <th> <%= link_to "Released", sort: "released" %> </th> <th></th> <th></th> <th></th> </tr> :