From 1e0bf57c63a0795c2cb8f26b63febb07dd64fea9 Mon Sep 17 00:00:00 2001 From: dev Date: Fri, 29 Jun 2012 13:50:47 +0900 Subject: [PATCH] I finished. --- .rspec | 2 +- app/controllers/books_controller.rb | 3 +++ app/models/book.rb | 1 + app/views/books/show.html.erb | 3 ++- config/routes.rb | 4 +++- db/schema.rb | 9 ++++++++- spec/models/book_spec.rb | 5 +++++ 7 files changed, 23 insertions(+), 4 deletions(-) diff --git a/.rspec b/.rspec index 89b89ff..3309142 100644 --- a/.rspec +++ b/.rspec @@ -1,2 +1,2 @@ --colour ---format=d \ No newline at end of file +--format=d diff --git a/app/controllers/books_controller.rb b/app/controllers/books_controller.rb index d13ffd9..dfdbd69 100644 --- a/app/controllers/books_controller.rb +++ b/app/controllers/books_controller.rb @@ -16,6 +16,9 @@ def index # GET /books/1 # GET /books/1.json def show + @book = Book.find(params[:id]) + @memo = @book.memos.build + respond_to do |format| format.html # show.html.erb format.xml { render xml: @book } diff --git a/app/models/book.rb b/app/models/book.rb index 95c42dd..d654b4c 100644 --- a/app/models/book.rb +++ b/app/models/book.rb @@ -1,5 +1,6 @@ # encoding: UTF-8 class Book < ActiveRecord::Base + has_many :memos attr_accessible :memo, :purchased_on, :title validates :title, :presence => true diff --git a/app/views/books/show.html.erb b/app/views/books/show.html.erb index 23005ab..0804824 100644 --- a/app/views/books/show.html.erb +++ b/app/views/books/show.html.erb @@ -15,6 +15,7 @@ <%= @book.purchased_on %>

+<%= render 'memos/index' %> +<%= render 'memos/form' %> -<%= link_to 'Edit', edit_book_path(@book) %> | <%= link_to 'Back', books_path %> diff --git a/config/routes.rb b/config/routes.rb index f3d38ab..32492b6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,7 @@ BookMemo2::Application.routes.draw do - resources :books + resources :books do + resources :memos + end # The priority is based upon order of creation: # first created -> highest priority. diff --git a/db/schema.rb b/db/schema.rb index 5fc61c2..eb92665 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20120526050801) do +ActiveRecord::Schema.define(:version => 20120627131716) do create_table "books", :force => true do |t| t.string "title" @@ -21,4 +21,11 @@ t.datetime "updated_at", :null => false end + create_table "memos", :force => true do |t| + t.integer "book_id" + t.text "memo" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false + end + end diff --git a/spec/models/book_spec.rb b/spec/models/book_spec.rb index f820855..41b617a 100644 --- a/spec/models/book_spec.rb +++ b/spec/models/book_spec.rb @@ -2,6 +2,11 @@ require 'spec_helper' describe Book do + it 'タイトルが未入力の場合はエラー' do + book = Book.new + book.valid?.should be_false + end + describe 'validation' do describe 'title' do subject { Book.new.valid? }