学习ruby on rails 笔记(第一版)

我用的 Rails 和 Ruby 的版本:
D:\work\depot>rails -v
Rails 1.2.6

D:\work\depot>ruby -v
ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32]

第六章 任务A 货品维护

rails depot

ruby script/server scaffold Product Admin

ruby script/server

http://localhost:3000/admin


depot 数据库的脚本
D:\work>mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 12 to server version: 5.0.27-community-nt

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

建立数据库:
create database depot_development;
create database depot_test;
create database depot_production;

D:\work\depot>mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7 to server version: 5.0.27-community-nt

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> grant all on depot_development.* to 'root'@'localhost';
Query OK, 0 rows affected (0.00 sec)

mysql> grant all on depot_test.* to 'root'@'localhost';
Query OK, 0 rows affected (0.00 sec)

mysql> grant all on depot_production.* to 'root'@'localhost';
Query OK, 0 rows affected (0.00 sec)

删除数据库:
drop database depot_development;
drop database depot_test;
drop database depot_production;

mysql> use mysql
Database changed
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| depot_development |
| depot_production |
| depot_test |
| mysql |
+--------------------+
5 rows in set (0.00 sec)

mysql> drop database depot_development;
Query OK, 1 row affected (0.06 sec)

mysql> drop database depot_test;
Query OK, 0 rows affected (0.00 sec)

mysql> drop database depot_production;
Query OK, 0 rows affected (0.00 sec)

mysql>

显示所有数据库:
show databases;

选择数据库:
use depot_development

mysql> use depot_development
Database changed
mysql> use mysql
Database changed
mysql> use depot_development; 这个地方可要';' 也可不要';'
Database changed

显示所有表:
show tables;

创建货品表(create_0.sql 的内容):
drop table if exists products;
create table products (
id int not null auto_increment,
title varchar(100) not null,
description text not null,
image_url varchar(200) not null,
price decimal(10,2) not null,
primary key(id)
);

执行 SQL 语句:
建表:
mysql depot_development <db/create.sql
删除表中数据:
mysql> delete from products;
Query OK, 4 rows affected (0.03 sec)

D:\work\depot>mysql depot_development <db/create.sql (没有指定用户出错了)
ERROR 1045 (28000): Access denied for user 'ODBC'@'localhost' (using password: NO)

D:\work\depot>mysql -u root depot_development <db/create.sql

D:\work\depot>mysql -u root depot_development <db/product_data.sql (导入数据)

D:\work\depot>mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10 to server version: 5.0.27-community-nt

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> use depot_development
Database changed
mysql> show tables;
+-----------------------------+
| Tables_in_depot_development |
+-----------------------------+
| products |
+-----------------------------+
1 row in set (0.00 sec)

mysql> select * from products;
Empty set (0.00 sec)

mysql>


添加缺失的字段 :
alter table products add column date_available datetime; 这个是以前老的方法,不建议采用

创建货品表(create_1.sql 的内容):
drop table if exists products;
create table products (
id int not null auto_increment,
title varchar(100) not null,
description text not null,
image_url varchar(200) not null,
price decimal(10,2) not null,
date_available datetime not null,
primary key(id)
);


product.rb

class Product < ActiveRecord::Base
validates_presence_of :title, :description, :image_url
validates_numericality_of :price
validates_uniqueness_of :title
validates_format_of :image_url,
:with => %r{^http:.+\.(gif|jpg|png)$}i,
:message => "must be a URL for a GIF, JPG, or PNG image"
protected
def validate
errors.add(:price, "should be positive") unless price.nil? || price >= 0.01
end
end



D:\work\depot\app\views\admin\list.rhtml


<h1>Products Listing </h1>
<table cellpadding="5" cellspacing="0">
<%
odd_or_even = 0
for product in @products
odd_or_even = 1 - odd_or_even
%>
<tr valign="top" class="ListLine<%= odd_or_even %>">
<td>
<img width="60" height="70" src="<%= product.image_url %>"/>
</td>
<td width="60%">
<span class="ListTitle"><%= h(product.title) %></span><br />
<%= h(truncate(product.description, 80)) %>
</td>
<td allign="right">
<%= product.date_availbale.strftime("%y-%m-%d") %><br />
<strong>$<%= sprintf("%0.2f", product.price) %></strong>
</td>
<td class="ListActions">
<%= link_to 'Show', :action => 'show', :id => product %><br />
<%= link_to 'Edit', :action => 'edit', :id => product %><br />
<%= link_to 'Destroy', { :action => 'destroy', :id => product }, :confirm => 'Are you sure?', :method => :post %>
</td>
<tr>
<% end %>
</table>
<%= if @product_pages.current.previous
link_to ('Previous page', { :page => @product_pages.current.previous })
end
%>
<%= if @product_pages.current.next
link_to ('Next page', { :page => @product_pages.current.next })
end
%>
<br />
<%= link_to 'New product', :action => 'new' %>


修改样式文件
D:\work\depot\public\stylesheets\scaffold.css


.ListTitle {
color: #244;
font-weight: bold;
font-size: larger;
}

.ListActions {
font-size: x-small;
text-align: right;
padding-left: lem;
}

.ListLine0 {
background: #e0f8f8;
}

.ListLine1 {
background: #f8b0f8;
}


新增货品的时候输入的图片地址
http://localhost:3000/images/svn.JPG
http://localhost:3000/images/utc.jpg
http://localhost:3000/images/auto.jpg

第七章 任务B 分类显示

ruby script/generate controller Store index

D:\work\depot\app\controllers\store_controller.rb


class StoreController < ApplicationController

def index
@products = Product.salable_items
end
end


D:\work\depot\app\models\product.rb

class Product < ActiveRecord::Base
validates_presence_of :title, :description, :image_url
validates_numericality_of :price
validates_uniqueness_of :title
validates_format_of :image_url,
:with => %r{^http:.+\.(gif|jpg|png)$}i,
:message => "must be a URL for a GIF, JPG, or PNG image"
protected
def validate
errors.add(:price, "should be positive") unless price.nil? || price >= 0.01
end

def self.salable_items
find(:all,
:conditions => "date_available <= now()",
:order => "date_available desc")
end
end


D:\work\depot\app\views\store\index.rhtml

<table cellpadding="5" cellspacing="0">
<% for product in @products %>
<tr valign="top">
<td>
<img src="<%= product.image_url %>"/>
</td>
<td width="450">
<h3><%=h product.title %></h3>
<small>
<%= product.description %>
</small>
<br />
<strong>$<%= sprintf("%0.2f", product.price) %></strong>
<%= link_to 'Add to Cart',
:action => 'add_to_cart',
:id => product %>
<br />
</td>
</tr>
<tr><td colspan="2"><hr/></td></tr>
<% end %>
</table>


D:\work\depot\app\views\layouts\store.rhtml

<html>
<head>
<title>Pragprog Books Online Store</title>
<%= stylesheet_link_tag "depot", :media => "all" %>
</head>
<body>
<div id="banner">
<img src="/images/logo.png" />
<%= @page_title || "Pragmatic Bookshelf" %>
</div>
<div id="columns">
<div id="side">
<a href="http://www....">Home</a><br />
<a href="http://www..../faq">Questions</a><br />
<a href="http://www..../news">News</a><br />
<a href="http://www..../contact">Contact</a><br />
</div>
<div id="main">
<%= @content_for_layout %>
</div>
</div>
</body>
</html>


D:\work\depot\app\views\store\index.rhtml

<% for product in @products %>
<div class="catalogentry">
<img src="<%= product.image_url %>"/>
<h3><%=h product.title %></h3>
<%= product.description %>
<span class="catalogprice">$<%= sprintf("%0.2f", product.price) %></span>
<%= link_to 'Add to Cart',
{:action => 'add_to_cart', :id => product },
:class => 'addtocart' %><br />
<div class="separator"> </div>
<% end %>
<%= link_to "Show my cart", :action => "display_cart" %>


D:\work\depot\public\stylesheets\depot.css

/* Global styles */

/* START:notice */
#notice {
border: 2px solid red;
padding: 1em;
margin-bottom: 2em;
background-color: #f0f0f0;
font: bold smaller sans-serif;
}
/* END:notice */

/* Styles for admin/list */

#product-list .list-title {
color: #244;
font-weight: bold;
font-size: larger;
}

#product-list .list-image {
width: 60px;
height: 70px;
}


#product-list .list-actions {
font-size: x-small;
text-align: right;
padding-left: 1em;
}

#product-list .list-line-even {
background: #e0f8f8;
}

#product-list .list-line-odd {
background: #f8b0f8;
}


/* Styles for main page */

#banner {
background: #9c9;
padding-top: 10px;
padding-bottom: 10px;
border-bottom: 2px solid;
font: small-caps 40px/40px "Times New Roman", serif;
color: #282;
text-align: center;
}

#banner img {
float: left;
}

#columns {
background: #141;
}

#main {
margin-left: 15em;
padding-top: 4ex;
padding-left: 2em;
background: white;
}

#side {
float: left;
padding-top: 1em;
padding-left: 1em;
padding-bottom: 1em;
width: 14em;
background: #141;
}

#side a {
color: #bfb;
font-size: small;
}

h1 {
font: 150% sans-serif;
color: #226;
border-bottom: 3px dotted #77d;
}

/* And entry in the store catalog */

#store .entry {
border-bottom: 1px dotted #77d;
}

#store .title {
font-size: 120%;
font-family: sans-serif;
}

#store .entry img {
width: 75px;
float: left;
}


#store .entry h3 {
margin-bottom: 2px;
color: #227;
}

#store .entry p {
margin-top: 0px;
margin-bottom: 0.8em;
}

#store .entry .price-line {
}

#store .entry .add-to-cart {
position: relative;
}

#store .entry .price {
color: #44a;
font-weight: bold;
margin-right: 2em;
}

/* START:inline */
#store .entry form, #store .entry form div {
display: inline;
}
/* END:inline */

/* START:cart */
/* Styles for the cart in the main page and the sidebar */

.cart-title {
font: 120% bold;
}

.item-price, .total-line {
text-align: right;
}

.total-line .total-cell {
font-weight: bold;
border-top: 1px solid #595;
}


/* Styles for the cart in the sidebar */

#cart, #cart table {
font-size: smaller;
color: white;

}

#cart table {
border-top: 1px dotted #595;
border-bottom: 1px dotted #595;
margin-bottom: 10px;
}
/* END:cart */

/* Styles for order form */

.depot-form fieldset {
background: #efe;
}

.depot-form legend {
color: #dfd;
background: #141;
font-family: sans-serif;
padding: 0.2em 1em;
}

.depot-form label {
width: 5em;
float: left;
text-align: right;
margin-right: 0.5em;
display: block;
}

.depot-form .submit {
margin-left: 5.5em;
}

/* The error box */

.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;
}


第八章 任务C: 创建购物车

D:\work\depot\app\controllers\store_controller.rb

class StoreController < ApplicationController

def index
@products = Product.salable_items
end

private
def find_cart
session[:cart] ||= Cart.new
end
end



drop table if exists line_items;
create table line_items (
id int not null auto_increment,
product_id int not null,
quantity int not null default 0,
unit_price decimal(10,2) not null,
constraint fk_items_product foreign key (product_id) references products(id),
primary key (id)
);


D:\work\depot\db\product_data.sql


lock tables products write;
insert into products( title, description, image_url, price, date_available ) values(
'Pragmatic Project Automation',
'A really great read!',
'http://localhost:3000/images/svn.JPG',
'29.95',
'2007-12-25 05:00:00' );
insert into products( title, description, image_url, price, date_available ) values(
'Pragmatic Version Control',
'A really contrlooed read!',
'http://localhost:3000/images/utc.jpg',
'29.95',
'2007-12-01 05:00:00');
insert into products( title, description, image_url, price, date_available ) values(
'Pragmatic Version Control2',
'A really contrlooed read!',
'http://localhost:3000/images/auto.jpg',
'29.95',
'2007-12-01 05:00:00');
unlock tables;


D:\work\depot>mysql -u root depot_development <db/product_data.sql (导入数据)

下面说明在mysql中 exit 和 quit 命令都可以退出 mysql
D:\work\depot>mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 41 to server version: 5.0.27-community-nt

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> use depot_development
Database changed
mysql> delete from products;
Query OK, 3 rows affected (0.05 sec)

mysql> exit
Bye

D:\work\depot>mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 42 to server version: 5.0.27-community-nt

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> quit
Bye

D:\work\depot>

D:\work\depot>ruby script/generate model LineItem

D:\work\depot\app\models\line_item.rb

class LineItem < ActiveRecord::Base
belongs_to :product
end


D:\work\depot\app\controllers\store_controller.rb

def add_to_cart
product = Product.find(params[:id])
@cart = find_cart
@cart.add_product(product)
redirect_to(:action => 'display_cart')
end


D:\work\depot\app\models\cart.rb

class Cart
attr_reader :items
attr_reader :total_price
def initialize
@items = []
@total_price = 0.0
end
def add_product(product)
@items << LineItem.for_product(product)
@total_price += product.price
end
end


D:\work\depot\app\models\line_item.rb

class LineItem < ActiveRecord::Base
belongs_to :product
def self.for_product(product)
item = self.new
item.quantity = 1
item.product = product
item.unit_price = product.price
item
end
end


D:\work\depot\app\controllers\store_controller.rb

def display_cart
@cart = find_cart
@items = @cart.items
end


D:\work\depot\app\views\store\display_cart.rhtml

<h1>Display Cart</h1>
<p>
Your cart contains <%= @items.size %> items.
</p>


D:\work\depot\app\controllers\application.rb

# Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.

class ApplicationController < ActionController::Base
model :cart
model :line_item
# Pick a unique cookie name to distinguish our session data from others'
session :session_key => '_depot_session_id'
end


D:\work\depot\app\views\store\display_cart.rhtml


<h1>Display Cart</h1>
<table>
<% for item in @items
product = item.product
-%>
<tr>
<td><%= item.quantity %></td>
<td><%= h(product.title) %></td>
<td align="right"><%= item.unit_price %></td>
<td aling="right"><%= item.unit_price * item.quantity %></td>
</tr>
<% end -%>
<table>


D:\work\depot\app\models\cart.rb


class Cart
attr_reader :items
attr_reader :total_price
def initialize
@items = []
@total_price = 0.0
end
def add_product(product)
item = @items.find {|i| i.product_id == product.id}
if item
item.quantity += 1
else
item = LineItem.for_product(product)
@items << item
end
@total_price += product.price
end
end


美化购物车

D:\work\depot\app\views\store\display_cart.rhtml


<div id="cartmenu">
<u1>
<li><%= link_to 'Continue shopping', :action => "index" %></li>
<li><%= link_to 'Empty car', :action => "empty_cart" %></li>
<li><%= link_to 'Checkout', :action => "checkout" %></li>
</u1>
</div>
<table cellpadding="10" cellspacing="0">
<tr class="carttitle">
<td rowspan="2">Qty</td>
<td rowspan="2">Description</td>
<td colspan="2">Price</td>
</tr>
<tr class="carttitle">
<td>Each</td>
<td>Total</td>
</tr>
<%
for item in @items
product = item.product
-%>
<tr>
<td><%= item.quantity %></td>
<td><%= h(product.title) %></td>
<td align="right"><%= item.unit_price %></td>
<td align="right"><%=item.unit_price * item.quantity %></td>
</tr>
<% end %>
<tr>
<td colspan="3" align="right"><strong>Total:</strong></td>
<td id="totalcell"><%= @cart.total_price %></td>
</tr>
</table>


处理错误

D:\work\depot\app\controllers\store_controller.rb


def add_to_cart
product = Product.find(params[:id])
@cart = find_cart
@cart.add_product(product)
redirect_to(:action => 'display_cart')
rescue
logger.error("Attempt to access invalid product #{params[:id]}")
flash[:notice] = 'Invalid product'
redirect_to(:action => 'index')
end


D:\work\depot\log\development.log

D:\work\depot\app\views\layouts\store.rhtml

<html>
<head>
<title>Pragprog Books Online Store</title>
<%= stylesheet_link_tag "depot", :media => "all" %>
</head>
<body>
<div id="banner">
<img src="/images/logo.png" />
<%= @page_title || "Pragmatic Bookshelf" %>
</div>
<div id="columns">
<div id="side">
<a href="http://www....">Home</a><br />
<a href="http://www..../faq">Questions</a><br />
<a href="http://www..../news">News</a><br />
<a href="http://www..../contact">Contact</a><br />
</div>
<div id="main">
<% if @flash[:notice] -%>
<div id="notice"><%= @flash[:notice] %></div>
<% end -%>
<%= @content_for_layout %>
</div>
</div>
</body>
</html>


D:\work\depot\app\controllers\store_controller.rb


class StoreController < ApplicationController

def index
@products = Product.salable_items
end

def add_to_cart
product = Product.find(params[:id])
@cart = find_cart
@cart.add_product(product)
redirect_to(:action => 'display_cart')
rescue
logger.error("Attempt to access invalid product #{params[:id]}")
flash[:notice] = 'Invalid product'
redirect_to(:action => 'index')
end

def display_cart
@cart = find_cart
@items = @cart.items
if @items.empty?
flash[:notice] = "Your cart is currently empty"
redirect_to(:action => 'index')
end
end

private
def find_cart
session[:cart] ||= Cart.new
end
end


D:\work\depot\app\views\store\display_cart.rhtml


<% @page_title = "Your Pragmatic Cart" -%>
<div id="cartmenu">
<u1>
<li><%= link_to 'Continue shopping', :action => "index" %></li>
<li><%= link_to 'Empty car', :action => "empty_cart" %></li>
<li><%= link_to 'Checkout', :action => "checkout" %></li>
</u1>
</div>
<table cellpadding="10" cellspacing="0">
<tr class="carttitle">
<td rowspan="2">Qty</td>
<td rowspan="2">Description</td>
<td colspan="2">Price</td>
</tr>
<tr class="carttitle">
<td>Each</td>
<td>Total</td>
</tr>
<%
for item in @items
product = item.product
-%>
<tr>
<td><%= item.quantity %></td>
<td><%= h(product.title) %></td>
<td align="right"><%= item.unit_price %></td>
<td align="right"><%=item.unit_price * item.quantity %></td>
</tr>
<% end %>
<tr>
<td colspan="3" align="right"><strong>Total:</strong></td>
<td id="totalcell"><%= @cart.total_price %></td>
</tr>
</table>


完成购物车

D:\work\depot\app\controllers\store_controller.rb

def empty_cart
@cart = find_cart
@cart.empty!
flash[:notice] = 'Your cart is now empty'
redirect_to(:action => 'index')
end


D:\work\depot\app\models\cart.rb

def empty!
@items = []
@total_price = 0.0
end


D:\work\depot\app\controllers\store_controller.rb


class StoreController < ApplicationController

def index
@products = Product.salable_items
end

def add_to_cart
product = Product.find(params[:id])
@cart = find_cart
@cart.add_product(product)
redirect_to(:action => 'display_cart')
rescue
logger.error("Attempt to access invalid product #{params[:id]}")
redirect_to_index(:action => 'Invalid product')
end

def empty_cart
@cart = find_cart
@cart.empty!
redirect_to_index(:action => 'Your cart is now empty')
end

def display_cart
@cart = find_cart
@items = @cart.items
if @items.empty?
redirect_to_index('Your cart is currently empty')
end
end

private
def find_cart
session[:cart] ||= Cart.new
end
def redirect_to_index(msg = nil)
flash[:notice] = msg if msg
redirect_to(:action => 'index')
end
end


D:\work\depot\app\models\cart.rb

class Cart
attr_reader :items
attr_reader :total_price
def initialize
empty!
end

def empty!
@items = []
@total_price = 0.0
end

def add_product(product)
item = @items.find {|i| i.product_id == product.id}
if item
item.quantity += 1
else
item = LineItem.for_product(product)
@items << item
end
@total_price += product.price
end
end


辅助方法

D:\work\depot\app\helpers\application_helper.rb

# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
def fmt_dollars(amt)
sprintf("$%0.2f", amt)
end
end


D:\work\depot\app\views\store\display_cart.rhtml

<% @page_title = "Your Pragmatic Cart" -%>
<div id="cartmenu">
<u1>
<li><%= link_to 'Continue shopping', :action => "index" %></li>
<li><%= link_to 'Empty car', :action => "empty_cart" %></li>
<li><%= link_to 'Checkout', :action => "checkout" %></li>
</u1>
</div>
<table cellpadding="10" cellspacing="0">
<tr class="carttitle">
<td rowspan="2">Qty</td>
<td rowspan="2">Description</td>
<td colspan="2">Price</td>
</tr>
<tr class="carttitle">
<td>Each</td>
<td>Total</td>
</tr>
<%
for item in @items
product = item.product
-%>
<tr>
<td><%= item.quantity %></td>
<td><%= h(product.title) %></td>
<td align="right"><%= fmt_dollars(item.unit_price) %></td>
<td align="right"><%= fmt_dollars(item.unit_price * item.quantity) %></td>
</tr>
<% end %>
<tr>
<td colspan="3" align="right"><strong>Total:</strong></td>
<td id="totalcell"><%= fmt_dollars(@cart.total_price) %></td>
</tr>
</table>


D:\work\depot\app\views\store\index.rhtml

<% for product in @products %>
<div class="catalogentry">
<img src="<%= product.image_url %>"/>
<h3><%=h product.title %></h3>
<%= product.description %>
<span class="catalogprice"><%= fmt_dollars(product.price) %></span>
<%= link_to 'Add to Cart',
{:action => 'add_to_cart', :id => product },
:class => 'addtocart' %><br />
<div class="separator"> </div>
<% end %>
<%= link_to "Show my cart", :action => "display_cart" %>


第九章 任务D: 结账

D:\work\depot\db\create.sql

drop table if exists line_items;
drop table if exists orders;
drop table if exists products;

create table products (
id int not null auto_increment,
title varchar(100) not null,
description text not null,
image_url varchar(200) not null,
price decimal(10,2) not null,
date_available datetime not null,
primary key(id)
);

create table orders (
id int not null auto_increment,
name varchar(100) not null,
email varchar(255) not null,
address text not null,
pay_type char(10) not null,
primary key (id)
);

create table line_items (
id int not null auto_increment,
product_id int not null,
order_id int not null,
quantity int not null default 0,
unit_price decimal(10,2) not null,
constraint fk_items_product foreign key (product_id) references products(id),
constraint fk_items_order foreign key (order_id) references orders(id),
primary key (id)
);


D:\work\depot\app\models\order.rb

class Order < ActiveRecord::Base
has_many :line_items
end


D:\work\depot\app\models\line_item.rb

class LineItem < ActiveRecord::Base
belongs_to :product
belongs_to :order
def self.for_product(product)
item = self.new
item.quantity = 1
item.product = product
item.unit_price = product.price
item
end
end


D:\work\depot\app\controllers\store_controller.rb

def checkout
@cart = find_cart
@items = @cart.items
if @items.empty?
redirect_to_index("There's nothing in your cart!")
else
@order = Order.new
end
end


D:\work\depot\app\views\store\checkout.rhtml

<% @page_title = "Checkout" -%>
<%= start_form_tag(:action => "save_order") %>
<table>
<tr>
<td>Name:</td>
<td><%= text_field("order", "name", "size" => 40) %></td>
</tr>
<tr>
<td>EMail:</td>
<td><%= text_field("order", "email", "size" => 40) %></td>
</tr>
<tr valign="top">
<td>Address:</td>
<td><%= text_area("order", "address", "cols" => 40, "rows" => 5) %></td>
</tr>
<tr>
<td>Pay using:</td>
<td><%=
options = [["Select a payment option", ""]] + Order::PAYMENT_TYPES
select("order", "pay_type", options) %></td>
</tr>
<tr>
<td></td>
<td><%= submit_tag(" CHECKOUT ") %></td>
</tr>
</table>
<%= end_form_tag %>


D:\work\depot\app\models\order.rb

class Order < ActiveRecord::Base
has_many :line_items

PAYMENT_TYPES = [
[ "Check", "check"],
[ "Credit Card", "cc"],
[ "Purchas Order", "po"]
].freeze # freeze to make this array constant
end


D:\work\depot\app\controllers\store_controller.rb

class StoreController < ApplicationController

def index
@products = Product.salable_items
end

def add_to_cart
product = Product.find(params[:id])
@cart = find_cart
@cart.add_product(product)
redirect_to(:action => 'display_cart')
rescue
logger.error("Attempt to access invalid product #{params[:id]}")
redirect_to_index(:action => 'Invalid product')
end

def empty_cart
@cart = find_cart
@cart.empty!
redirect_to_index(:action => 'Your cart is now empty')
end

def display_cart
@cart = find_cart
@items = @cart.items
if @items.empty?
redirect_to_index('Your cart is currently empty')
end
end

def checkout
@cart = find_cart
@items = @cart.items
if @items.empty?
redirect_to_index("There's nothing in your cart!")
else
@order = Order.new
end
end

def save_order
@cart = find_cart
@order = Order.new(params[:order])
@order.line_items << @cart.items
if @order.save
@cart.empty!
redirect_to_index('Thank you for your order.')
else
render(:action => 'checkout')
end
end

private
def find_cart
session[:cart] ||= Cart.new
end
def redirect_to_index(msg = nil)
flash[:notice] = msg if msg
redirect_to(:action => 'index')
end
end


D:\work\depot\app\models\order.rb

class Order < ActiveRecord::Base
has_many :line_items

PAYMENT_TYPES = [
[ "Check", "check"],
[ "Credit Card", "cc"],
[ "Purchas Order", "po"]
].freeze # freeze to make this array constant

validates_presence_of :name, :email, :address, :pay_type

end


D:\work\depot\app\views\layouts\store.rhtml

<html>
<head>
<title>Pragprog Books Online Store</title>
<%= stylesheet_link_tag "scaffold", "depot", :media => "all" %>
</head>
<body>
<div id="banner">
<img src="/images/logo.png" />
<%= @page_title || "Pragmatic Bookshelf" %>
</div>
<div id="columns">
<div id="side">
<a href="http://www....">Home</a><br />
<a href="http://www..../faq">Questions</a><br />
<a href="http://www..../news">News</a><br />
<a href="http://www..../contact">Contact</a><br />
</div>
<div id="main">
<% if @flash[:notice] -%>
<div id="notice"><%= @flash[:notice] %></div>
<% end -%>
<%= @content_for_layout %>
</div>
</div>
</body>
</html>


mysql> select * from orders;
+----+------+---------------+----------------+----------+
| id | name | email | address | pay_type |
+----+------+---------------+----------------+----------+
| 3 | aaaa | bbbb@aaaa.com | aaaa bbbb cccc | cc |
+----+------+---------------+----------------+----------+
1 row in set (0.00 sec)

mysql> select * from line_items;
+----+------------+----------+----------+------------+
| id | product_id | order_id | quantity | unit_price |
+----+------------+----------+----------+------------+
| 1 | 1 | 3 | 1 | 29.95 |
| 2 | 2 | 3 | 1 | 29.95 |
| 3 | 3 | 3 | 1 | 29.95 |
+----+------------+----------+----------+------------+
3 rows in set (0.00 sec)

迭代D2: 在付账页面显示购物车内容

D:\work\depot\app\views\store\checkout.rhtml


<% @page_title = "Checkout" -%>
<%= error_messages_for("order") %>
<%= render_component(:acton => "display_cart") %>
<%= start_form_tag(:action => "save_order") %>
<table>
<tr>
<td>Name:</td>
<td><%= text_field("order", "name", "size" => 40) %></td>
</tr>
<tr>
<td>EMail:</td>
<td><%= text_field("order", "email", "size" => 40) %></td>
</tr>
<tr valign="top">
<td>Address:</td>
<td><%= text_area("order", "address", "cols" => 40, "rows" => 5) %></td>
</tr>
<tr>
<td>Pay using:</td>
<td><%=
options = [["Select a payment option", ""]] + Order::PAYMENT_TYPES
select("order", "pay_type", options) %></td>
</tr>
<tr>
<td></td>
<td><%= submit_tag(" CHECKOUT ") %></td>
</tr>
</table>
<%= end_form_tag %>



D:\work\depot\app\views\store\checkout.rhtml

<% @page_title = "Checkout" -%>
<%= error_messages_for("order") %>
<%= render_component(:acton => "display_cart",
:params => { :context => :checkout }) %>
<%= start_form_tag(:action => "save_order") %>
<table>
<tr>
<td>Name:</td>
<td><%= text_field("order", "name", "size" => 40) %></td>
</tr>
<tr>
<td>EMail:</td>
<td><%= text_field("order", "email", "size" => 40) %></td>
</tr>
<tr valign="top">
<td>Address:</td>
<td><%= text_area("order", "address", "cols" => 40, "rows" => 5) %></td>
</tr>
<tr>
<td>Pay using:</td>
<td><%=
options = [["Select a payment option", ""]] + Order::PAYMENT_TYPES
select("order", "pay_type", options) %></td>
</tr>
<tr>
<td></td>
<td><%= submit_tag(" CHECKOUT ") %></td>
</tr>
</table>
<%= end_form_tag %>


D:\work\depot\app\controllers\store_controller.rb

修改如下方法:

def display_cart
@cart = find_cart
@items = @cart.items
if @items.empty?
redirect_to_index('Your cart is currently empty')
end
if params[:context] == :checkout
render(:layout => false )
end
end


D:\work\depot\app\views\store\display_cart.rhtml

<% @page_title = "Your Pragmatic Cart" -%>
<div id="cartmenu">
<u1>
<li><%= link_to 'Continue shopping', :action => "index" %></li>
<% unless params[:context] == :checkout -%>
<li><%= link_to 'Empty car', :action => "empty_cart" %></li>
<li><%= link_to 'Checkout', :action => "checkout" %></li>
<% end -%>
</u1>
</div>
<table cellpadding="10" cellspacing="0">
<tr class="carttitle">
<td rowspan="2">Qty</td>
<td rowspan="2">Description</td>
<td colspan="2">Price</td>
</tr>
<tr class="carttitle">
<td>Each</td>
<td>Total</td>
</tr>
<%
for item in @items
product = item.product
-%>
<tr>
<td><%= item.quantity %></td>
<td><%= h(product.title) %></td>
<td align="right"><%= fmt_dollars(item.unit_price) %></td>
<td align="right"><%= fmt_dollars(item.unit_price * item.quantity) %></td>
</tr>
<% end %>
<tr>
<td colspan="3" align="right"><strong>Total:</strong></td>
<td id="totalcell"><%= fmt_dollars(@cart.total_price) %></td>
</tr>
</table>


D:\work\depot\app\views\store\checkout.rhtml

<% @page_title = "Checkout" -%>
<%= error_messages_for("order") %>
<%= render_component(:acton => "display_cart",
:params => { :context => :checkout }) %>
<h3>Please enter your details below</h3>
<%= start_form_tag(:action => "save_order") %>
<table>
<tr>
<td>Name:</td>
<td><%= text_field("order", "name", "size" => 40) %></td>
</tr>
<tr>
<td>EMail:</td>
<td><%= text_field("order", "email", "size" => 40) %></td>
</tr>
<tr valign="top">
<td>Address:</td>
<td><%= text_area("order", "address", "cols" => 40, "rows" => 5) %></td>
</tr>
<tr>
<td>Pay using:</td>
<td><%=
options = [["Select a payment option", ""]] + Order::PAYMENT_TYPES
select("order", "pay_type", options) %></td>
</tr>
<tr>
<td></td>
<td><%= submit_tag(" CHECKOUT ") %></td>
</tr>
</table>
<%= end_form_tag %>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在现有省、市港口信息化系统进行有效整合基础上,借鉴新 一代的感知-传输-应用技术体系,实现对码头、船舶、货物、重 大危险源、危险货物装卸过程、航管航运等管理要素的全面感知、 有效传输和按需定制服务,为行政管理人员和相关单位及人员提 供高效的管理辅助,并为公众提供便捷、实时的水运信息服务。 建立信息整合、交换和共享机制,建立健全信息化管理支撑 体系,以及相关标准规范和安全保障体系;按照“绿色循环低碳” 交通的要求,搭建高效、弹性、高可扩展性的基于虚拟技术的信 息基础设施,支撑信息平台低成本运行,实现电子政务建设和服务模式的转变。 实现以感知港口、感知船舶、感知货物为手段,以港航智能 分析、科学决策、高效服务为目的和核心理念,构建“智慧港口”的发展体系。 结合“智慧港口”相关业务工作特点及信息化现状的实际情况,本项目具体建设目标为: 一张图(即GIS 地理信息服务平台) 在建设岸线、港口、港区、码头、泊位等港口主要基础资源图层上,建设GIS 地理信息服务平台,在此基础上依次接入和叠加规划建设、经营、安全、航管等相关业务应用专题数据,并叠 加动态数据,如 AIS/GPS/移动平台数据,逐步建成航运管理处 "一张图"。系统支持扩展框架,方便未来更多应用资源的逐步整合。 现场执法监管系统 基于港口(航管)执法基地建设规划,依托统一的执法区域 管理和数字化监控平台,通过加强对辖区内的监控,结合移动平 台,形成完整的多维路径和信息追踪,真正做到问题能发现、事态能控制、突发问题能解决。 运行监测和辅助决策系统 对区域港口与航运业务日常所需填报及监测的数据经过科 学归纳及分析,采用统一平台,消除重复的填报数据,进行企业 输入和自动录入,并进行系统智能判断,避免填入错误的数据, 输入的数据经过智能组合,自动生成各业务部门所需的数据报 表,包括字段、格式,都可以根据需要进行定制,同时满足扩展 性需要,当有新的业务监测数据表需要产生时,系统将分析新的 需求,将所需字段融合进入日常监测和决策辅助平台的统一平台中,并生成新的所需业务数据监测及决策表。 综合指挥调度系统 建设以港航应急指挥中心为枢纽,以各级管理部门和经营港 口企业为节点,快速调度、信息共享的通信网络,满足应急处置中所需要的信息采集、指挥调度和过程监控等通信保障任务。 设计思路 根据项目的建设目标和“智慧港口”信息化平台的总体框架、 设计思路、建设内容及保障措施,围绕业务协同、信息共享,充 分考虑各航运(港政)管理处内部管理的需求,平台采用“全面 整合、重点补充、突出共享、逐步完善”策略,加强重点区域或 运输通道交通基础设施、运载装备、运行环境的监测监控,完善 运行协调、应急处置通信手段,促进跨区域、跨部门信息共享和业务协同。 以“统筹协调、综合监管”为目标,以提供综合、动态、实 时、准确、实用的安全畅通和应急数据共享为核心,围绕“保畅通、抓安全、促应急"等实际需求来建设智慧港口信息化平台。 系统充分整合和利用航运管理处现有相关信息资源,以地理 信息技术、网络视频技术、互联网技术、移动通信技术、云计算 技术为支撑,结合航运管理处专网与行业数据交换平台,构建航 运管理处与各部门之间智慧、畅通、安全、高效、绿色低碳的智 慧港口信息化平台。 系统充分考虑航运管理处安全法规及安全职责今后的变化 与发展趋势,应用目前主流的、成熟的应用技术,内联外引,优势互补,使系统建设具备良好的开放性、扩展性、可维护性。
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值