Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .rvmrc

This file was deleted.

3 changes: 3 additions & 0 deletions app/assets/javascripts/memos.js.coffee
Original file line number Diff line number Diff line change
@@ -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/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/memos.css.scss
Original file line number Diff line number Diff line change
@@ -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/
3 changes: 3 additions & 0 deletions app/controllers/books_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
66 changes: 66 additions & 0 deletions app/controllers/memos_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
class MemosController < ApplicationController

before_filter :load_book

def load_book
@book = Book.find(params[:book_id])
end

# GET /memos
# GET /memos.json
def index
@memos = @book.memos.all

respond_to do |format|
format.html # index.html.erb
format.json { render json: @memos }
end
end

# GET /memos/1
# GET /memos/1.json
def show
end

# GET /memos/new
# GET /memos/new.json
def new
end

# GET /memos/1/edit
def edit
end

# POST /memos
# POST /memos.json
def create
@memo = @book.memos.build(params[:memo])

respond_to do |format|
if @memo.save
format.html { redirect_to @book, notice: 'Memo was successfully created.' }
format.json { render json: @memo, status: :created, location: @memo }
else
format.html { render 'books/show' }
format.json { render json: @memo.errors, status: :unprocessable_entity }
end
end
end

# PUT /memos/1
# PUT /memos/1.json
def update
end

# DELETE /memos/1
# DELETE /memos/1.json
def destroy
@memo = @book.memos.find(params[:id])
@memo.destroy

respond_to do |format|
format.html { redirect_to book_url(@book) }
format.json { head :no_content }
end
end
end
2 changes: 2 additions & 0 deletions app/helpers/memos_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module MemosHelper
end
1 change: 1 addition & 0 deletions app/models/book.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# encoding: UTF-8
class Book < ActiveRecord::Base
has_many :memos
attr_accessible :memo, :purchased_on, :title
validates :title, :presence => true

Expand Down
7 changes: 7 additions & 0 deletions app/models/memo.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Memo < ActiveRecord::Base
belongs_to :book
attr_accessible :body

validates :body, :presence => true, :length => { :maximum => 100 }

end
4 changes: 3 additions & 1 deletion app/views/books/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
<%= @book.purchased_on %>
</p>


<%= link_to 'Edit', edit_book_path(@book) %> |
<%= link_to 'Back', books_path %>
<hr />
<%= render 'memos/index' %>
<%= render 'memos/form' %>
21 changes: 21 additions & 0 deletions app/views/memos/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<%= form_for([@book, @memo]) do |f| %>
<% if @memo.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@memo.errors.count, "error") %> prohibited this memo from being saved:</h2>

<ul>
<% @memo.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="field">
<%= f.label :body %><br />
<%= f.text_area :body %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
8 changes: 8 additions & 0 deletions app/views/memos/_index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<h2>Memos</h2>
<ul>
<% @book.memos.each do |memo| %>
<% unless memo.id.nil? %>
<li><%= memo.body %> <%= link_to 'Destory', book_memo_path(@book, memo), confirm: 'r u sure?', method: :delete %></li>
<% end %>
<% end %>
</ul>
6 changes: 6 additions & 0 deletions app/views/memos/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1>Editing memo</h1>

<%= render 'form' %>

<%= link_to 'Show', [@book, @memo] %> |
<%= link_to 'Back', book_memos_path %>
26 changes: 26 additions & 0 deletions app/views/memos/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<h1>Listing memos</h1>

<table>
<tr>
<th>Book</th>
<th>Body</th>
<th></th>
<th></th>
<th></th>
</tr>

<% @memos.each do |memo| %>
<tr>
<td><%= memo.book %></td>
<td><%= memo.body %></td>
<td><%= link_to 'Show', book_memo_path(@book, memo) %></td>
<td><%= link_to 'Edit', edit_book_memo_path(@book, memo) %></td>
<td><%= link_to 'Destroy', book_memo_path(@book, memo), confirm: 'Are you sure?', method: :delete %></td>
</tr>
<% end %>
</table>

<br />

<%= link_to 'Back', books_path %> |
<%= link_to 'New Memo', new_book_memo_path %>
5 changes: 5 additions & 0 deletions app/views/memos/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h1>New memo</h1>

<%= render 'form' %>

<%= link_to 'Back', book_memos_path %>
15 changes: 15 additions & 0 deletions app/views/memos/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<p id="notice"><%= notice %></p>

<p>
<b>Book:</b>
<%= @memo.book %>
</p>

<p>
<b>Body:</b>
<%= @memo.body %>
</p>


<%= link_to 'Edit', edit_book_memo_path %> |
<%= link_to 'Back', book_memos_path %>
4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
11 changes: 11 additions & 0 deletions db/migrate/20120614103230_create_memos.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateMemos < ActiveRecord::Migration
def change
create_table :memos do |t|
t.references :book
t.text :body

t.timestamps
end
add_index :memos, :book_id
end
end
20 changes: 19 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 => 20120615004432) do

create_table "books", :force => true do |t|
t.string "title"
Expand All @@ -21,4 +21,22 @@
t.datetime "updated_at", :null => false
end

create_table "comments", :force => true do |t|
t.integer "book_id"
t.text "memo"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end

add_index "comments", ["book_id"], :name => "index_comments_on_book_id"

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

add_index "memos", ["book_id"], :name => "index_memos_on_book_id"

end
Loading