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
3 changes: 3 additions & 0 deletions app/assets/javascripts/books.js.coffee
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# 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/



22 changes: 22 additions & 0 deletions app/assets/javascripts/books.js.coffee~
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# 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/

function addMemo() {
idNumber++;
//这里我们创建一个label控件
label0 = document.createElement("label");
label0.setAttribute("id","label"+idNumber);
//创建输入框
var textField = document.createElement("input");
textField.setAttribute("type","text");
textField.setAttribute("name","memo");
textField.setAttribute("id","memo"+idNumber);
label0.appendChild(textField);

document.getElementById(id).appendChild(label0);
id= "memo"+idNumber;
}



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/
11 changes: 4 additions & 7 deletions app/controllers/books_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,7 @@ def show
# 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
@book.memos.build
end

# GET /books/1/edit
Expand All @@ -43,7 +38,9 @@ def edit
# POST /books.json
def create
@book = Book.new(params[:book])

params[:memos].each do |memo|
@book.memos.build(memo)
end
respond_to do |format|
if @book.save
format.html { redirect_to @book, notice: 'Book was successfully created.' }
Expand Down
89 changes: 89 additions & 0 deletions app/controllers/books_controller.rb~
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
class BooksController < ApplicationController
before_filter :find_book, :only => [:show, :destroy, :edit, :update]

# GET /books
# 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
@book.memos.build
end

# GET /books/1/edit
def edit
end

# POST /books
# POST /books.json
def create
@book = Book.new(params[:book])
params[:memos].each_value do |memo|
@book.memos.build(memo)
end
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 }
else
format.html { render action: "new" }
format.xml { render xml: @book.errors, status: :unprocessable_entity }
format.json { render json: @book.errors, status: :unprocessable_entity }
end
end
end

# 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 }
else
format.html { render action: "edit" }
format.xml { render xml: @book.errors, status: :unprocessable_entity }
format.json { render json: @book.errors, status: :unprocessable_entity }
end
end
end

# DELETE /books/1
# DELETE /books/1.json
def destroy
@book.destroy

respond_to do |format|
format.html { redirect_to books_url }
format.xml { head :no_content }
format.json { head :no_content }
end
end

private
def find_book
@book = Book.find(params[:id])
end
end
83 changes: 83 additions & 0 deletions app/controllers/memos_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
class MemosController < ApplicationController
# GET /memos
# GET /memos.json
def index
@memos = Memo.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
@memo = Memo.find(params[:id])

respond_to do |format|
format.html # show.html.erb
format.json { render json: @memo }
end
end

# GET /memos/new
# GET /memos/new.json
def new
@memo = Memo.new

respond_to do |format|
format.html # new.html.erb
format.json { render json: @memo }
end
end

# GET /memos/1/edit
def edit
@memo = Memo.find(params[:id])
end

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

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

# PUT /memos/1
# PUT /memos/1.json
def update
@memo = Memo.find(params[:id])

respond_to do |format|
if @memo.update_attributes(params[:memo])
format.html { redirect_to @memo, notice: 'Memo was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @memo.errors, status: :unprocessable_entity }
end
end
end

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

respond_to do |format|
format.html { redirect_to memos_url }
format.json { head :no_content }
end
end
end
1 change: 1 addition & 0 deletions app/helpers/books_helper.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
module BooksHelper

end
9 changes: 9 additions & 0 deletions app/helpers/books_helper.rb~
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module BooksHelper
def add_task_link name
link_to_function name , nil do |page|
page.insert_html :bottom, :memos, :partial => 'memo', :object => Memo.new
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
15 changes: 11 additions & 4 deletions app/models/book.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
# encoding: UTF-8
class Book < ActiveRecord::Base
attr_accessible :memo, :purchased_on, :title

has_many :memos, :dependent => :destroy
accepts_nested_attributes_for :memos
attr_accessible :purchased_on, :title, :memos, :id
validates :title, :presence => true

before_create :total_books_count

def total_books_count
self.memo += "【 累計冊数#{Book.count + 1} 】"

def new_memo_attributes=(memo_attributes)
memo_attributes.each do |attributes|
memos.build(attributes)
end
end


end
18 changes: 18 additions & 0 deletions app/models/book.rb~
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# encoding: UTF-8
class Book < ActiveRecord::Base

has_many :memos, :dependent => :destroy
accepts_nested_attributes_for :memos
attr_accessible :purchased_on, :title, :memos, :id
validates :title, :presence => true



def new_memo_attributes=(memo_attributes)
memo_attributes.each do |attributes|
memos.build(attributes)
end
end


end
8 changes: 8 additions & 0 deletions app/models/memo.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Memo < ActiveRecord::Base
belongs_to :Book, :foreign_key => "book_id"
attr_accessible :book_id, :memo
validates_length_of :memo, :maximum => 100


end

12 changes: 12 additions & 0 deletions app/models/memo.rb~
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Memo < ActiveRecord::Base
belongs_to :Book, :foreign_key => "book_id"
attr_accessible :book_id, :memo
validates_length_of :memo, :maximum => 100

before_create :total_books_count

def total_books_count
self.memo += "【 累計冊数#{Book.count + 1} 】"
end
end

19 changes: 15 additions & 4 deletions app/views/books/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@
</div>
<% end %>

<div class="field">
<div class="field" id="title">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :memo %><br />
<%= f.text_area :memo %>
<div id="memos">
Memo:<br>
<input name="memos[][memo]" size="30" type="text"/><br>
<button id="addYunyun" type="button" onClick="yunyun()">Add Memo</button>

</div>


<div class="field">
<%= f.label :purchased_on %><br />
<%= f.date_select :purchased_on %>
Expand All @@ -27,3 +31,10 @@
<%= f.submit %>
</div>
<% end %>
<script>
var yunyun = function(){
$('#addYunyun').before('<input name="memos[][memo]" size="30" type="text"/><br>');
}
</script>


40 changes: 40 additions & 0 deletions app/views/books/_form.html.erb~
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<%= form_for(@book) do |f| %>
<% if @book.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@book.errors.count, "error") %> prohibited this book from being saved:</h2>

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

<div class="field" id="title">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div id="memos">
Memo:<br>
<input name="memos[][memo]" size="30" type="text"/><br>
<button id="addYunyun" type="button" onClick="yunyun()">Add Memo</button>

</div>


<div class="field">
<%= f.label :purchased_on %><br />
<%= f.date_select :purchased_on %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<script>
var yunyun = function(){
$('#addYunyun').before('<input name="memos[][memo]" size="30" type="text"/><br>');
}
</script>


Loading