Tbpgr Blog

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

Ruby on Rails | 標準的な登録画面の作成

概要

標準的な登録画面の作成

内容

書籍を登録する画面を作成します。
登録情報は以下。
書籍名:テキスト
ISBN-CODE:最大13桁テキスト
税抜き価格:数値(整数)

DB定義

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    |                |
+------------+--------------+------+-----+---------+----------------+

DBサンプルデータ

mysql> select * from books;
+----+----------------------------------------------------------------+----------------+-------+---------------------+---------------------+
| id | name                                                           | isbn           | price | created_at          | updated_at          |
+----+----------------------------------------------------------------+----------------+-------+---------------------+---------------------+
|  1 | RailsによるアジャイルWebアプリケーション開発                   | 9784274068669 |  3200 | 2013-07-09 15:08:46 | 2013-07-09 15:08:46 |
|  2 | メタプログラミングRuby                                         | 9784048687157 |  2800 | 2013-07-09 15:08:46 | 2013-07-09 15:08:46 |
|  3 | 入門bash                                                       | 9784873112541 |  2800 | 2013-07-11 13:49:15 | 2013-07-11 13:49:15 |
+----+----------------------------------------------------------------+----------------+-------+---------------------+---------------------+

製造

Model Book
class Book < ActiveRecord::Base
  validates :name, presence: true, :uniqueness => true
  validates :isbn, presence: true, :uniqueness => true
  validates :price, presence: true
end
Controller
class BookController < ApplicationController
  def list
    @books = Book.find(:all)
  end

  def new
    @messages = flash[:messages]
    @book = flash[:book] ||= Book.new
  end

  def create
    book = Book.create do |b|
      b.name = params[:name]
      b.isbn = params[:isbn]
      b.price = params[:price]
    end
    if book.errors.any?
      flash[:messages] = book.errors.full_messages
      flash[:book] = book
      return redirect_to :action=>'new', :book => book
    end
    redirect_to :action=>'list'
  end
end
View
%h1 Book#new
%p Find me in app/views/book/new.html.haml
%hr/ 
= link_to '戻る', :controller => 'book', :action => 'list'
%hr/ 
- unless @messages.blank?
  %div
    - @messages.each do |m|
      %p 
        =m
    %hr/ 
=form_tag("/book/create") do
  %p 
    =label_tag 'name', 'BookName'
    =text_field_tag 'name', @book.name
  %p 
    =label_tag 'isbn', 'ISBN-CODE'
    =text_field_tag 'isbn', @book.isbn
  %p 
    =label_tag 'price', 'Price'
    =text_field_tag 'price', @book.price
  %p 
    =submit_tag 'submit'

画像

新規登録前一覧

新規登録表示

新規登録表示、必須チェックエラー

新規登録表示、正常データ送信前

新規登録前一覧、追加データの表示確認