十一 28

重读AWDWR笔记

分类:tech | | 给我留言

感恩节放假,抓紧时间重读一下AWDWR把一些知识点记录如下。
第6章

  • 迁移习惯 create 来创建表,add 给现有表增加字段。你可以会看到 002_add_price.rb 的迁移。
  • model 中验证方法设置为 protected 是因为该方法必须在特定的模型上下文中调用,不能在外部调用。
protected
def validate
  errors.add(:price, "should be at least 0.01") if price.nil? || price < 0.01
end
  • errors.add() 方法第一个参数是字段名称,第二个参数是出错信息的正文。
  • 在将价格和 0.01 比较之前,先检查它是不是 nil。试图将 nil 和数字比较会引发异常。
  • 下面代码演示了如何用正则表达式验证模型属性。
validates_format_of :image_url,
  :with => %r{\.(gif|jpg|png)$}i,
  :message => "must be a URL for a GIF, JPG, or PNG image"
  • cycle 交替的设置两个属性。
  • h() 方法用于对内容进行格式化,去除其中的 HTML。
  • link_to 方法后面的 :confirm =>"Are you sure?"。
  • stylesheet_link_tag
  • 关于渲染表格的方法。
<table>
	<tr>
	<% for column in Product.content_columns %>
		<th&gt<%= column.hunman_name %></th>
	<% end %>
	</tr>
	<tr>
	<% for column in Product.content_columns %>
		<td&gt<%= h product.send(column.name) %></td>
	<% end %>
	</tr>
</table>

第7章

  • number_to_currency(product.price) 格式化价格的方法。
  • 如何使用类方法。
class StoreController < ApplicationController
  def inde
    @products = Product.find_products_sale
  end
end

class Product < ActiveRecord:Base
  def self.find_products_sale
    find(:all, :o rder => "title")
  end
end

第8章

  • 如何把 session 放在数据库中。
  • 在 session 中保存尽可能简单的东西:字符串,数字,等等。应用层面的对象应该放在数据库,然后把它们的主键放入 session,需要时根据 session 中的主键来查找对象。
rake db:sessions:create
rake db:migrate
#environment.rb
config.action_controller.sesson_store = :active_recored_store
  • 下面是一段非常常见的购物代码。
  • 注意下面的 controller 中演示了如何防止构造错误的传递参数。
class StoreController < ApplicationController
  before_filter :find_cart, :except => :empty_cart

  def index
    @products = Product.find_product_for_sale
  end

  def add_to_cart
    begin
      product = Product.find(params[:id])
    rescue ActiveRecord::RecordNotFound
      logger.error("Attempt to access invalid product #{params[:id]}.")
      redirect_to_index unless request.xhr?
    end
  end

  def empty_cart
    session[:cart] = nil
    redirect_to_index
  end

  def checkout
    if @cart.item.empty?
      redirect_to_index("Your cart is empty.")
    else
      @order = Order.new
    end
  end

  def save_order
    @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

  private

  def redirect_to_index(msg = nil)
    flash[:notice] = msg if msg
    redirect_to :action => :index
  end

  def find_cart
    @cart = (session[:cart] ||= Cart.new)
  end
end

class Cart
  attr_reader :items

  def initialize
    @items = []
  end

  def add_product(product)
    current_item = @items.find {|item| item.product == product}
    if current_item
      current_item.increment_quantity
    else
      current_item = CartItem.new(product)
      @items << current_item
    end
    current_item
  end

  def total_items
    @items.sum {|item| item.quantity}
  end

  def total_price
    @items.sum {|item| item.price}
  end
end

class CartItem
  attr_reader :product, :quantity

  def initialize(product)
    @product = product
    @quantity = 1
  end

  def increment_quantity
    @quantity += 1
  end

  def title
    @product.title
  end

  def price
    @product.price * @quantity
  end
end
  • Mac 用户使用 Console.app,在 Application 的 Utilities 中,可以很方便的跟踪日志文件,只要使用 open 命令,并传入日志文件的名称即可。
  • 标签 : 
  • 原文链接 : http://railser.cn/index.php/blog/awdwr-1
  • 转载原创文章请注明 : 里克的自习室
  • 收藏到 : Google书签 新浪ViVi 365Key网摘 天极网摘 我摘 POCO网摘 博采网摘 YouNote网摘 和讯网摘 博拉网 igooi网摘 I2Key网摘 天下图摘 百特门网摘 Del.icio.us Yahoo书签 奇贴 QQ娱乐摘 添加到Digg! 添加到Facebook!
  • 发表留言