<%= product.title %>
<%= sanitize(product.description) %>
<%= number_to_currency(product.price) %>
<%= button_to 'Add to Cart', line_items_path(product_id: product) %>
rails generate scaffold cart
rake db:migrate
rails generate scaffold line_item product_id:integer cart_id:integer
rake db:migrate
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
private
def current_cart
Cart.find(session[:cart_id])
rescue ActiveRecord::RecordNotFound
cart = Cart.create
session[:cart_id] = cart.id
cart
end
end
class Cart < ActiveRecord::Base
has_many :line_items, dependent: :destroy
end
一个Cart含有多个(has_many)line_items,因为每个line_item都会有一个cart_id对应。当我们destroy一个cart,并从数据库删除时,相应与cart关联的line item也要被删除。
class LineItem < ActiveRecord::Base
belongs_to :product
belongs_to :cart
end
belongs_to告诉Rails,在line_items table中的行是carts和products table中的行的children。其实就是foreign key的设置。
class Product < ActiveRecord::Base
has_many :line_items
before_destroy :ensure_not_referenced_by_any_line_item
validates :description, :image_url, presence: true
validates :price, numericality: {greater_than_or_equal_to: 0.01}
validates :title, uniqueness: true
validates :image_url, allow_blank: true, format: {
with: %r{\.(gif|jpg|png)\Z}i,
message: 'must be a URL for GIF, JPG or PNG image.'
}
private
# ensure that there are no line items referencing this product
def ensure_not_referenced_by_any_line_item
if line_items.empty?
return true
else
errors.add(:base, 'Line Items present')
return false
end
end
end
ensure_not_referenced_by_any_line_item方法是一个hook 方法,即在数据库destroy这个row之前,Rails先调用的方法,如果返回false则不destroy。这里我们可以直接访问errors,它也是validate得到的errors,当然error可以和一个属性联系在一起(error[:title]),但这里就和object联系在一起了。
<% if notice %>
<%= notice %>
<% end %>
Your Pragmatic Catalog
<% @products.each do |product| %>
<%= image_tag(product.image_url) %>
<%= product.title %>
<%= sanitize(product.description) %>
<%= number_to_currency(product.price) %>
<%= button_to 'Add to Cart', line_items_path(product_id: product) %>
<% end %>
p, div.price_line {
margin-left: 100px;
margin-top: 0.5em;
margin-bottom: 0.8em;
form,div {
display: inline;
}
}
这样添加了form,并inline显示。
def create
@cart = current_cart #调用application_controller中定义的current_cart方法获得当前的cart,如果没有就新建
product = Product.find(params[:product_id]) # parmas是从browser URL request中获取的参数,根据product的id,找到当前选择的product。
@line_item = @cart.line_items.build #新建line_item
@line_item.product = product #line_item的product为当前的product
respond_to do |format|
if @line_item.save #保存成功的话,跳转到cart
format.html { redirect_to @line_item.cart, notice: 'Line item was successfully created.' }
format.json { render :show, status: :created, location: @line_item }
else
format.html { render :new }
format.json { render json: @line_item.errors, status: :unprocessable_entity }
end
end
end
test "should create line_item" do
assert_difference('LineItem.count') do
# post :create, line_item: { cart_id: @line_item.cart_id, product_id: @line_item.product_id }
post :create, product_id: products(:ruby).id
end
#assert_redirected_to line_item_path(assigns(:line_item))
assert_redirected_to line_item_path(assigns(:line_item).cart)
end
运行测试:
rake test:functionals
<% if notice %>
<%= notice %>
<% end %>
Your Pragmatic Cart
<% @cart.line_items.each do |item| %>
- <%= item.product.title %>
<% end %>
<%= link_to 'Edit', edit_cart_path(@cart) %> |
<%= link_to 'Back', carts_path %>