diff --git a/Gemfile b/Gemfile
index d643000..0c26841 100644
--- a/Gemfile
+++ b/Gemfile
@@ -44,3 +44,5 @@ group :test, :development do
end
gem 'pry-rails'
gem 'factory_girl_rails'
+gem 'execjs'
+gem 'therubyracer'
diff --git a/Gemfile.lock b/Gemfile.lock
index 0f24858..d523488 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -67,6 +67,7 @@ GEM
json (1.7.3)
launchy (2.1.0)
addressable (~> 2.2.6)
+ libv8 (3.3.10.4)
libwebsocket (0.1.3)
addressable
mail (2.4.4)
@@ -140,6 +141,8 @@ GEM
rack (~> 1.0)
tilt (~> 1.1, != 1.3.0)
sqlite3 (1.3.6)
+ therubyracer (0.10.1)
+ libv8 (~> 3.3.10)
thor (0.14.6)
tilt (1.3.3)
treetop (1.4.10)
@@ -158,6 +161,7 @@ PLATFORMS
DEPENDENCIES
capybara
coffee-rails (~> 3.2.1)
+ execjs
factory_girl_rails
jquery-rails
launchy
@@ -166,4 +170,5 @@ DEPENDENCIES
rspec-rails (~> 2.0)
sass-rails (~> 3.2.3)
sqlite3
+ therubyracer
uglifier (>= 1.0.3)
diff --git a/Rakefile b/Rakefile
index 5dc0f03..a4a0a9e 100644
--- a/Rakefile
+++ b/Rakefile
@@ -3,5 +3,5 @@
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
require File.expand_path('../config/application', __FILE__)
-
+require 'rake'
BookMemo2::Application.load_tasks
diff --git a/app/assets/javascripts/memo.js.coffee b/app/assets/javascripts/memo.js.coffee
new file mode 100644
index 0000000..7615679
--- /dev/null
+++ b/app/assets/javascripts/memo.js.coffee
@@ -0,0 +1,3 @@
+# Place all the behaviors and hooks related to the matching controller here.
+# All this logic will automatically be available in application.js.
+# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
diff --git a/app/assets/javascripts/memos.js.coffee b/app/assets/javascripts/memos.js.coffee
new file mode 100644
index 0000000..7615679
--- /dev/null
+++ b/app/assets/javascripts/memos.js.coffee
@@ -0,0 +1,3 @@
+# Place all the behaviors and hooks related to the matching controller here.
+# All this logic will automatically be available in application.js.
+# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
diff --git a/app/assets/stylesheets/memo.css.scss b/app/assets/stylesheets/memo.css.scss
new file mode 100644
index 0000000..8b1e873
--- /dev/null
+++ b/app/assets/stylesheets/memo.css.scss
@@ -0,0 +1,3 @@
+// Place all the styles related to the memo controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
diff --git a/app/assets/stylesheets/memos.css.scss b/app/assets/stylesheets/memos.css.scss
new file mode 100644
index 0000000..2b49bc5
--- /dev/null
+++ b/app/assets/stylesheets/memos.css.scss
@@ -0,0 +1,3 @@
+// Place all the styles related to the memos controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
diff --git a/app/controllers/books_controller.rb b/app/controllers/books_controller.rb
index d13ffd9..c026881 100644
--- a/app/controllers/books_controller.rb
+++ b/app/controllers/books_controller.rb
@@ -1,3 +1,5 @@
+#coding: utf-8
+
class BooksController < ApplicationController
before_filter :find_book, :only => [:show, :destroy, :edit, :update]
@@ -5,34 +7,17 @@ class BooksController < ApplicationController
# GET /books.json
def index
@books = Book.all
-
- respond_to do |format|
- format.html # index.html.erb
- format.xml { render xml: @books }
- format.json { render json: @books }
- end
end
# GET /books/1
# GET /books/1.json
def show
- respond_to do |format|
- format.html # show.html.erb
- format.xml { render xml: @book }
- format.json { render json: @book }
- end
end
# GET /books/new
# GET /books/new.json
def new
@book = Book.new
-
- respond_to do |format|
- format.html # new.html.erb
- format.xml { render xml: @book }
- format.json { render json: @book }
- end
end
# GET /books/1/edit
@@ -46,13 +31,9 @@ def create
respond_to do |format|
if @book.save
- format.html { redirect_to @book, notice: 'Book was successfully created.' }
- format.xml { render xml: @book, status: :created, location: @book }
- format.json { render json: @book, status: :created, location: @book }
+ redirect_to @book, :notice => "Completed Registering"
else
- format.html { render action: "new" }
- format.xml { render xml: @book.errors, status: :unprocessable_entity }
- format.json { render json: @book.errors, status: :unprocessable_entity }
+ render :new
end
end
end
@@ -60,17 +41,11 @@ def create
# PUT /books/1
# PUT /books/1.json
def update
- respond_to do |format|
if @book.update_attributes(params[:book])
- format.html { redirect_to @book, notice: 'Book was successfully updated.' }
- format.xml { head :no_content }
- format.json { head :no_content }
+ redirect_to @book, :notice =>"Completed Updating"
else
- format.html { render action: "edit" }
- format.xml { render xml: @book.errors, status: :unprocessable_entity }
- format.json { render json: @book.errors, status: :unprocessable_entity }
+ render :edit
end
- end
end
# DELETE /books/1
diff --git a/app/controllers/memos_controller.rb b/app/controllers/memos_controller.rb
new file mode 100644
index 0000000..708f3d1
--- /dev/null
+++ b/app/controllers/memos_controller.rb
@@ -0,0 +1,45 @@
+#coding: utf-8
+
+class MemosController < ApplicationController
+ before_filter :find_book
+ before_filter :find_memo, :only =>[:edit, :update, :destroy]
+
+ def new
+ @memo = @book.memos.new
+ end
+
+ def create
+ @memo = @book.memos.new(params[:memo])
+ if @memo.save
+ redirect_to book_path(@book), :notice => "Completed Registering Memo."
+ else
+ render :new
+ end
+ end
+
+ def edit
+ end
+
+ def update
+ if @memo.update_attributes(params[:memo])
+ redirect_to book_path(@book), :notice => "メモを更新しました。"
+ else
+ render :action => "edit"
+ end
+ end
+
+ def destroy
+ @memo.destroy
+ redirect_to book_path(@book)
+ end
+
+ private
+ def find_book
+ @book = Book.find(params[:book_id])
+ end
+
+ def find_memo
+ @memo = @book.memos.find(params[:id])
+ end
+
+end
diff --git a/app/helpers/memos_helper.rb b/app/helpers/memos_helper.rb
new file mode 100644
index 0000000..0e732a1
--- /dev/null
+++ b/app/helpers/memos_helper.rb
@@ -0,0 +1,2 @@
+module MemosHelper
+end
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/models/memo.rb b/app/models/memo.rb
new file mode 100644
index 0000000..f26393c
--- /dev/null
+++ b/app/models/memo.rb
@@ -0,0 +1,3 @@
+class Memo < ActiveRecord::Base
+ attr_accessible :body, :book_id
+end
diff --git a/app/views/books/.new.html.erb.swp b/app/views/books/.new.html.erb.swp
new file mode 100644
index 0000000..1f56917
Binary files /dev/null and b/app/views/books/.new.html.erb.swp differ
diff --git a/app/views/books/edit.html.erb b/app/views/books/edit.html.erb
index a907cb4..6e37cec 100644
--- a/app/views/books/edit.html.erb
+++ b/app/views/books/edit.html.erb
@@ -1,6 +1,6 @@
Editing book
-<%= render 'form' %>
+<%= render 'form', :book => @book %>
<%= link_to 'Show', @book %> |
<%= link_to 'Back', books_path %>
diff --git a/app/views/books/new.html.erb b/app/views/books/new.html.erb
index c9042b2..04d410e 100644
--- a/app/views/books/new.html.erb
+++ b/app/views/books/new.html.erb
@@ -1,5 +1,5 @@
New book
-<%= render 'form' %>
+<%= render 'form', book => @book %>
<%= link_to 'Back', books_path %>
diff --git a/app/views/books/show.html.erb b/app/views/books/show.html.erb
index 23005ab..1075972 100644
--- a/app/views/books/show.html.erb
+++ b/app/views/books/show.html.erb
@@ -15,6 +15,13 @@
<%= @book.purchased_on %>
+<%= link_to 'add memo', new_book_memo_path(@book) %>
+<% @book.memos.each do |memo| %>
+ <%= memo.body %> |
+ <%= link_to "edit", edit_book_memo_path(:book_id =>@book.id, :id => memo.id) %> |
+ <%= link_to "delete", book_memo_path(:book_id => @book.id,:id => memo.id), :confirm =>"Are you sure?", :method => :delete %>
+<%end%>
+
<%= link_to 'Edit', edit_book_path(@book) %> |
<%= link_to 'Back', books_path %>
diff --git a/app/views/memos/edit.html.erb b/app/views/memos/edit.html.erb
new file mode 100644
index 0000000..2321b06
--- /dev/null
+++ b/app/views/memos/edit.html.erb
@@ -0,0 +1,8 @@
+Title:<%= @book.title %>
+Memo:<%= @book.memo %>
+Parchase Date:<%= @book.purchased_on %>
+
+<%= form_for @memo, :url => book_memo_path(:book_id => @book.id, :id => @memo.id), :method => :put do |f| %>
+ Memo:<%= f.text_field :body %>
+ <%= f.submit %>
+<% end %>
diff --git a/app/views/memos/new.html.erb b/app/views/memos/new.html.erb
new file mode 100644
index 0000000..f64b6d2
--- /dev/null
+++ b/app/views/memos/new.html.erb
@@ -0,0 +1,8 @@
+Title:<%= @book.title %>
+Memo:<%= @book.memo %>
+Parchase Date:<%= @book.purchased_on %>
+
+<%= form_for @memo, :url => book_memos_path do |f| %>
+ Memo:<%= f.text_field :body %>
+ <%= f.submit %>
+<% end %>
diff --git a/config/routes.rb b/config/routes.rb
index f3d38ab..0a28312 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -1,5 +1,10 @@
BookMemo2::Application.routes.draw do
- resources :books
+
+ root :to => 'books#index'
+
+ resources :books do
+ resources :memos
+ end
# The priority is based upon order of creation:
# first created -> highest priority.
diff --git a/db/migrate/20120617023553_create_memos.rb b/db/migrate/20120617023553_create_memos.rb
new file mode 100644
index 0000000..fb34767
--- /dev/null
+++ b/db/migrate/20120617023553_create_memos.rb
@@ -0,0 +1,14 @@
+class CreateMemos < ActiveRecord::Migration
+ def change
+ create_table :memos do |t|
+ t.integer :book_id
+ t.text :body
+
+ t.timestamps
+ end
+ end
+
+ def self.down
+ drop_table :memos
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 5fc61c2..b913d25 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 => 20120617023553) 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 "body"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
+
end
diff --git a/public/index.html b/public/index.html
deleted file mode 100644
index a1d5099..0000000
--- a/public/index.html
+++ /dev/null
@@ -1,241 +0,0 @@
-
-
-
- Ruby on Rails: Welcome aboard
-
-
-
-
-
-
-
-
-
-
-
-
-
-
Getting started
-
Here’s how to get rolling:
-
-
- -
-
Use rails generate to create your models and controllers
- To see all available options, run it without parameters.
-
-
- -
-
Set up a default route and remove public/index.html
- Routes are set up in config/routes.rb.
-
-
- -
-
Create your database
- Run rake db:create to create your database. If you're not using SQLite (the default), edit config/database.yml with your username and password.
-
-
-
-
-
-
-
-
-
diff --git a/spec/controllers/memo_controller_spec.rb b/spec/controllers/memo_controller_spec.rb
new file mode 100644
index 0000000..edcbaae
--- /dev/null
+++ b/spec/controllers/memo_controller_spec.rb
@@ -0,0 +1,5 @@
+require 'spec_helper'
+
+describe MemoController do
+
+end
diff --git a/spec/controllers/memos_controller_spec.rb b/spec/controllers/memos_controller_spec.rb
new file mode 100644
index 0000000..9cbbcaf
--- /dev/null
+++ b/spec/controllers/memos_controller_spec.rb
@@ -0,0 +1,5 @@
+require 'spec_helper'
+
+describe MemosController do
+
+end
diff --git a/spec/factories/memos.rb b/spec/factories/memos.rb
new file mode 100644
index 0000000..8d3526d
--- /dev/null
+++ b/spec/factories/memos.rb
@@ -0,0 +1,8 @@
+# Read about factories at https://github.com/thoughtbot/factory_girl
+
+FactoryGirl.define do
+ factory :memo do
+ book_id 1
+ body "MyText"
+ end
+end
diff --git a/spec/helpers/memo_helper_spec.rb b/spec/helpers/memo_helper_spec.rb
new file mode 100644
index 0000000..5ea600f
--- /dev/null
+++ b/spec/helpers/memo_helper_spec.rb
@@ -0,0 +1,15 @@
+require 'spec_helper'
+
+# Specs in this file have access to a helper object that includes
+# the MemoHelper. For example:
+#
+# describe MemoHelper do
+# describe "string concat" do
+# it "concats two strings with spaces" do
+# helper.concat_strings("this","that").should == "this that"
+# end
+# end
+# end
+describe MemoHelper do
+ pending "add some examples to (or delete) #{__FILE__}"
+end
diff --git a/spec/helpers/memos_helper_spec.rb b/spec/helpers/memos_helper_spec.rb
new file mode 100644
index 0000000..48b8f0a
--- /dev/null
+++ b/spec/helpers/memos_helper_spec.rb
@@ -0,0 +1,15 @@
+require 'spec_helper'
+
+# Specs in this file have access to a helper object that includes
+# the MemosHelper. For example:
+#
+# describe MemosHelper do
+# describe "string concat" do
+# it "concats two strings with spaces" do
+# helper.concat_strings("this","that").should == "this that"
+# end
+# end
+# end
+describe MemosHelper do
+ pending "add some examples to (or delete) #{__FILE__}"
+end
diff --git a/spec/models/memo_spec.rb b/spec/models/memo_spec.rb
new file mode 100644
index 0000000..c4d756c
--- /dev/null
+++ b/spec/models/memo_spec.rb
@@ -0,0 +1,5 @@
+require 'spec_helper'
+
+describe Memo do
+ pending "add some examples to (or delete) #{__FILE__}"
+end
diff --git a/spec/requests/memos_spec.rb b/spec/requests/memos_spec.rb
new file mode 100644
index 0000000..a57a6df
--- /dev/null
+++ b/spec/requests/memos_spec.rb
@@ -0,0 +1,33 @@
+# -*- coding: utf-8 -*-
+require 'spec_helper'
+
+describe "/books/:id/memos/new" do
+
+ describe '/books/:id/memos/new' do
+ let!(:book){ FactoryGirl.create :book }
+ subject { page }
+
+ before { visit "/books/#{book.id}" }
+ it "タイトルが設定されていること" do
+ find("#book_title").value.should == book.title
+ end
+
+ context "with update" do
+ let(:insert_memo){ 'insert memo!' }
+
+ before do
+ fill_in "memo[body]", with: create_body
+ click_on 'Create Memo'
+ end
+
+ it "ページが遷移されていること" do
+ current_path.should == book_memo_path(book)
+ end
+
+ it "メモが追加されていること" do
+ should have_content book.memo.reload.body
+ book.memo.reload.body.should == create_body
+ end
+ end
+ end
+end