Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

Ruby on Rails | 標準的な一覧画面の作成

概要

標準的な一覧画面の作成

内容

書籍の一覧を作成します。

書籍テーブル定義
列名 データ型
id int(11)
name string
isbn string
price int(11)
created_at datetime
updated_at datetime
モデルを作成します
rails g model book name:string isbn:string price:integer
      invoke  active_record
      create    db/migrate/20130709150220_create_books.rb
      create    app/models/book.rb
      invoke    rspec
      create      spec/models/book_spec.rb
マイグレーションを実行します
rake db:migrate
==  CreateBooks: migrating ====================================================
-- create_table(:books)
   -> 0.0074s
==  CreateBooks: migrated (0.0075s) ===========================================
mysql> desc books;
+------------+--------------+------+-----+---------+----------------+
| Field      | Type         | Null | Key | Default | Extra          |
+------------+--------------+------+-----+---------+----------------+
| id         | int(11)      | NO   | PRI | NULL    | auto_increment |
| name       | varchar(255) | YES  |     | NULL    |                |
| isbn       | varchar(255) | YES  |     | NULL    |                |
| price      | int(11)      | YES  |     | NULL    |                |
| created_at | datetime     | YES  |     | NULL    |                |
| updated_at | datetime     | YES  |     | NULL    |                |
+------------+--------------+------+-----+---------+----------------+
seedsから初期データを投入します

db/seeds.rb

Book.create(name: 'RailsによるアジャイルWebアプリケーション開発', isbn: '978-4274068669', price: 3200)
Book.create(name: 'メタプログラミングRuby ', isbn: '978-4048687157', price: 2800)

初期データ投入

be rake db:seed

投入結果確認

mysql> select * from books;
+----+----------------------------------------------------------------+----------------+-------+---------------------+---------------------+
| id | name                                                           | isbn           | price | created_at          | updated_at          |
+----+----------------------------------------------------------------+----------------+-------+---------------------+---------------------+
|  1 | RailsによるアジャイルWebアプリケーション開発                   | 978-4274068669 |  3200 | 2013-07-09 15:08:46 | 2013-07-09 15:08:46 |
|  2 | メタプログラミングRuby                                         | 978-4048687157 |  2800 | 2013-07-09 15:08:46 | 2013-07-09 15:08:46 |
+----+----------------------------------------------------------------+----------------+-------+---------------------+---------------------+
2 rows in set (0.00 sec)
Controllerの生成
rails g controller book list
      create  app/controllers/book_controller.rb
       route  get "book/list"
      invoke  haml
      create    app/views/book
      create    app/views/book/list.html.haml
      invoke  rspec
      create    spec/controllers/book_controller_spec.rb
      create    spec/views/book
      create    spec/views/book/list.html.haml_spec.rb
      invoke  helper
      create    app/helpers/book_helper.rb
      invoke    rspec
      create      spec/helpers/book_helper_spec.rb
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/book.js.coffee
      invoke    scss
      create      app/assets/stylesheets/book.css.scss
Controllerの実装
class BookController < ApplicationController
  def list
    @books = Book.find(:all)
  end
end
Viewの実装
%h1 Book#list
%p Find me in app/views/book/list.html.haml
%hr/ 
%table 
  %tr 
    %th id
    %th name
    %th isbn
    %th price
    %th created_at
    %th updated_at
  -@books.each do |b|
    %tr 
      = content_tag :td, b.id
      = content_tag :td, b.name
      = content_tag :td, b.isbn
      = content_tag :td, b.price
      = content_tag :td, b.created_at
      = content_tag :td, b.updated_at
画面