-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
g scaffold book title:string price:integer
- Loading branch information
Showing
16 changed files
with
310 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
class BooksController < ApplicationController | ||
# GET /books | ||
# GET /books.xml | ||
def index | ||
@books = Book.all | ||
|
||
respond_to do |format| | ||
format.html # index.html.erb | ||
format.xml { render :xml => @books } | ||
end | ||
end | ||
|
||
# GET /books/1 | ||
# GET /books/1.xml | ||
def show | ||
@book = Book.find(params[:id]) | ||
|
||
respond_to do |format| | ||
format.html # show.html.erb | ||
format.xml { render :xml => @book } | ||
end | ||
end | ||
|
||
# GET /books/new | ||
# GET /books/new.xml | ||
def new | ||
@book = Book.new | ||
|
||
respond_to do |format| | ||
format.html # new.html.erb | ||
format.xml { render :xml => @book } | ||
end | ||
end | ||
|
||
# GET /books/1/edit | ||
def edit | ||
@book = Book.find(params[:id]) | ||
end | ||
|
||
# POST /books | ||
# POST /books.xml | ||
def create | ||
@book = Book.new(params[:book]) | ||
|
||
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 } | ||
else | ||
format.html { render :action => "new" } | ||
format.xml { render :xml => @book.errors, :status => :unprocessable_entity } | ||
end | ||
end | ||
end | ||
|
||
# PUT /books/1 | ||
# PUT /books/1.xml | ||
def update | ||
@book = Book.find(params[:id]) | ||
|
||
respond_to do |format| | ||
if @book.update_attributes(params[:book]) | ||
format.html { redirect_to(@book, :notice => 'Book was successfully updated.') } | ||
format.xml { head :ok } | ||
else | ||
format.html { render :action => "edit" } | ||
format.xml { render :xml => @book.errors, :status => :unprocessable_entity } | ||
end | ||
end | ||
end | ||
|
||
# DELETE /books/1 | ||
# DELETE /books/1.xml | ||
def destroy | ||
@book = Book.find(params[:id]) | ||
@book.destroy | ||
|
||
respond_to do |format| | ||
format.html { redirect_to(books_url) } | ||
format.xml { head :ok } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module BooksHelper | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
class Book < ActiveRecord::Base | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<% form_for(@book) do |f| %> | ||
<%= f.error_messages %> | ||
|
||
<div class="field"> | ||
<%= f.label :title %><br /> | ||
<%= f.text_field :title %> | ||
</div> | ||
<div class="field"> | ||
<%= f.label :price %><br /> | ||
<%= f.text_field :price %> | ||
</div> | ||
<div class="actions"> | ||
<%= f.submit %> | ||
</div> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<h1>Editing book</h1> | ||
|
||
<%= render 'form' %> | ||
<%= link_to 'Show', @book %> | | ||
<%= link_to 'Back', books_path %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<h1>Listing books</h1> | ||
|
||
<table> | ||
<tr> | ||
<th>Title</th> | ||
<th>Price</th> | ||
<th></th> | ||
<th></th> | ||
<th></th> | ||
</tr> | ||
|
||
<% @books.each do |book| %> | ||
<tr> | ||
<td><%= book.title %></td> | ||
<td><%= book.price %></td> | ||
<td><%= link_to 'Show', book %></td> | ||
<td><%= link_to 'Edit', edit_book_path(book) %></td> | ||
<td><%= link_to 'Destroy', book, :confirm => 'Are you sure?', :method => :delete %></td> | ||
</tr> | ||
<% end %> | ||
</table> | ||
|
||
<br /> | ||
|
||
<%= link_to 'New book', new_book_path %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<h1>New book</h1> | ||
|
||
<%= render 'form' %> | ||
<%= link_to 'Back', books_path %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<p> | ||
<b>Title:</b> | ||
<%= @book.title %> | ||
</p> | ||
|
||
<p> | ||
<b>Price:</b> | ||
<%= @book.price %> | ||
</p> | ||
|
||
|
||
<%= link_to 'Edit', edit_book_path(@book) %> | | ||
<%= link_to 'Back', books_path %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Books: <%= controller.action_name %></title> | ||
<%= stylesheet_link_tag 'scaffold' %> | ||
<%= javascript_include_tag :defaults %> | ||
<%= csrf_meta_tag %> | ||
</head> | ||
<body> | ||
|
||
<p class="notice"><%= notice %></p> | ||
|
||
<%= yield %> | ||
|
||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class CreateBooks < ActiveRecord::Migration | ||
def self.up | ||
create_table :books do |t| | ||
t.string :title | ||
t.integer :price | ||
|
||
t.timestamps | ||
end | ||
end | ||
|
||
def self.down | ||
drop_table :books | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
body { background-color: #fff; color: #333; } | ||
|
||
body, p, ol, ul, td { | ||
font-family: verdana, arial, helvetica, sans-serif; | ||
font-size: 13px; | ||
line-height: 18px; | ||
} | ||
|
||
pre { | ||
background-color: #eee; | ||
padding: 10px; | ||
font-size: 11px; | ||
} | ||
|
||
a { color: #000; } | ||
a:visited { color: #666; } | ||
a:hover { color: #fff; background-color:#000; } | ||
|
||
div.field, div.actions { | ||
margin-bottom: 10px; | ||
} | ||
|
||
.notice { | ||
color: green; | ||
} | ||
|
||
.fieldWithErrors { | ||
padding: 2px; | ||
background-color: red; | ||
display: table; | ||
} | ||
|
||
#errorExplanation { | ||
width: 400px; | ||
border: 2px solid red; | ||
padding: 7px; | ||
padding-bottom: 12px; | ||
margin-bottom: 20px; | ||
background-color: #f0f0f0; | ||
} | ||
|
||
#errorExplanation h2 { | ||
text-align: left; | ||
font-weight: bold; | ||
padding: 5px 5px 5px 15px; | ||
font-size: 12px; | ||
margin: -7px; | ||
background-color: #c00; | ||
color: #fff; | ||
} | ||
|
||
#errorExplanation p { | ||
color: #333; | ||
margin-bottom: 0; | ||
padding: 5px; | ||
} | ||
|
||
#errorExplanation ul li { | ||
font-size: 12px; | ||
list-style: square; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html | ||
|
||
one: | ||
title: MyString | ||
price: 1 | ||
|
||
two: | ||
title: MyString | ||
price: 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
require 'test_helper' | ||
|
||
class BooksControllerTest < ActionController::TestCase | ||
test "should get index" do | ||
get :index | ||
assert_response :success | ||
assert_not_nil assigns(:books) | ||
end | ||
|
||
test "should get new" do | ||
get :new | ||
assert_response :success | ||
end | ||
|
||
test "should create book" do | ||
assert_difference('Book.count') do | ||
post :create, :book => books(:one).attributes | ||
end | ||
|
||
assert_redirected_to book_path(assigns(:book)) | ||
end | ||
|
||
test "should show book" do | ||
get :show, :id => books(:one).to_param | ||
assert_response :success | ||
end | ||
|
||
test "should get edit" do | ||
get :edit, :id => books(:one).to_param | ||
assert_response :success | ||
end | ||
|
||
test "should update book" do | ||
put :update, :id => books(:one).to_param, :book => books(:one).attributes | ||
assert_redirected_to book_path(assigns(:book)) | ||
end | ||
|
||
test "should destroy book" do | ||
assert_difference('Book.count', -1) do | ||
delete :destroy, :id => books(:one).to_param | ||
end | ||
|
||
assert_redirected_to books_path | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
require 'test_helper' | ||
|
||
class BookTest < ActiveSupport::TestCase | ||
# Replace this with your real tests. | ||
test "the truth" do | ||
assert true | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
require 'test_helper' | ||
|
||
class BooksHelperTest < ActionView::TestCase | ||
end |