【ror学习笔记7】表单
一。创建模型
1.创建模型及数据表
depot>ruby script/generate model order
depot>ruby script/generate model line_item
2.修改数据表,添加表间关系
修改迁移006_create_orders.rb
class CreateOrders < ActiveRecord::Migration
def self.up
create_table :orders do |t|
t.column :name, :string
t.column :address, :text
t.column :email , :string
t.column :pay_type, :string, :limit => 10
end
end
def self.down
drop_table :orders
end
end
修改迁移007_create_line_items.rb
class CreateLineItems < ActiveRecord::Migration
def self.up
create_table :line_items do |t|
t.column :product_id, :integer, :null =>false
t.column :order_id, :integer, :null =>false
t.column :quantity, :integer, :null =>false
t.column :total_price, :decimal, :null =>false,
:precision =>8, :scale =>2
end
execute "alter table line_items add constraint
fk_line_item_products foreign key(product_id)
references products(id) "
execute "alter table line_items add constraint
fk_line_item_orders foreign key(order_id)
references orders(id) "
end
def self.down
drop_table :line_items
end
end
depot>rake dg:migrate
3.修改模型添加模型间关系
class Order < ActiveRecord::Base
has_many :line_items
end
class Product < ActiveRecord::Base
has_many :line_items
end
class LineItem < ActiveRecord::Base
belongs_to :order
belongs_to :product
end
二。创建表单搜集订单信息
1.在视图添加按钮
app\views\store\_cart.rhtml
<div class="cart-title">Your Cart </div>
<table>
<%= render(:partial =>"cart_item" , :collection => cart.items) %>
<tr class="total-line">
<td colspan = "2">Total</td>
<td class="total-cell"><%=number_to_currency(@cart.total_price) %></td>
</tr>
</table>
<%=button_to "Checkout", :action=>:checkout %>
<%= button_to "Empty cart" , :action=>:empty_cart%>
2.在控制器添加方法
store_controller.rb
def checkout
@cart = find_cart
if @cart.items.empty?
redirect_to_index("Your cart is empty")
else
@order = Order.new
end
end
3.添加方法的视图
views/store/checkout.rhtml
<div class="depot-form">
<%= error_messages_for 'order' %>
<fieldset>
<legend>Please Enter Your Details </legend>
<% form_for :order, :url=>{:action => :save_order} do |form| %>
<p>
<label for="order_name">Name:</label>
<%= form.text_field :name, :size => 40 %>
</p>
<p>
<label for="order_address">Address:</label>
<%= form.text_area :address, :rows => 3, :cols => 40 %>
</p>
<p>
<label for="order_email">E-mail:</label>
<%= form.text_field :email, :size => 40 %>
</p>
<p>
<label for="order_pay_type">Pay with:</label>
<%=
form.select :pay_type,
Order::PAYMENT_TYPES,
:prompt => "Select a payment method "
%>
</p>
<%= submit_tag "Place Order", :class => "submit" %>
<% end %>
</fieldset>
</div>
4.在模型中添加数据选择项目和数据校验
class Order < ActiveRecord::Base
has_many :line_items
PAYMENT_TYPES = [
#Displayed stored in db
["Check", "check"],
["Credit card" , "cc"],
["purchase order", "po"]
]
validates_presence_of :name, :address, :email, :pay_type
validates_inclusion_of :pay_type, :in =>PAYMENT_TYPES.map{|disp,value|value}
end
三。搜集表单信息存储到数据库
1.在视图添加按钮(已添加)
2.在控制器添加方法
store_controller.rb
def save_order
@cart = find_cart
@order = Order.new(params[:order])
@order.add_line_items_from_cart(@cart)
if @order.save
session[:cart] = nil
redirect_to_index("Thank you for your order")
else
render :action=>:checkout
end
end
3.在模型中添加相关方法
class Order < ActiveRecord::Base
has_many :line_items
PAYMENT_TYPES = [
#Displayed stored in db
["Check", "check"],
["Credit card" , "cc"],
["purchase order", "po"]
]
validates_presence_of :name, :address, :email, :pay_type
validates_inclusion_of :pay_type, :in =>PAYMENT_TYPES.map{|disp,value|value}
def add_line_items_from_cart (cart)
cart.items.each do |item|
li = LineItem.from_cart_item(item)
line_items << li
end
end
end
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
class LineItem < ActiveRecord::Base
belongs_to :order
belongs_to :product
def self.from_cart_item(cart_item)
li = self.new
li.product = cart_item.product
li.quantity = cart_item.quantity
li.total_price = cart_item.price
li
end
end