Rails读书笔记第九章

Cart Creation

建立scaffold

生成cart的scaffold:
rails generate scaffold cart
这样建立的cart model只有一个id属性,cart_id。
更新数据库:
rake db:migrate

为了将product和cart联系在一起,创建了line_item,一个在购物车中的对象。
新建line_item的scaffold:
rails generate scaffold line_item product_id:integer cart_id:integer
所以在line_item中有product的id和cart的id。
更新数据库:
rake db:migrate
这样在数据库中就可以存储product和cart的关系了。
为了让Rails应用存储这种关系,需要对Model做下面的修改。

在application controoler中获取cart

编辑app/controllers/application_controller.rb文件:
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
首先在sessoin中,查找cart_id,如果存在则返回这个cart,如果不存在则新建一个cart,放入到当前的session中。current_cart方法是private的。

Cart Model

修改app/models/cart.rb文件:
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也要被删除。
Line_item Model
修改app/models/line_item.rb文件:
class LineItem < ActiveRecord::Base
  belongs_to :product
  belongs_to :cart
end
belongs_to告诉Rails,在line_items table中的行是carts和products table中的行的children。其实就是foreign key的设置。

有了belongs_to后,我们就可以通过lineitem.product.title来访问商品的信息。
有了has_many后,我们就可以通过car.line_items.count来访问line_items的情况。

Product Model

如果有多cart,那么每个product最好存一下line items。
编辑app/models/product.rb文件:
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联系在一起了。

添加一个Button:

linke_to方法默认是调用HTTP的GET方法,要用POST可以使用button_to方法。
编辑app/views/store/index.html.erb文件:
<% 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 %>
使用line_items_path可以获得line_item_path的URL,并将product的id传过去,即这个URL对line_item的controller发出一个POST动作,参数为product的id。
这样就生成了HTML的form tag,包含div tag。我们想把这个block放在price旁边,下面就编辑app/assets/sytlesheets/store.css.scss:
    p, div.price_line {
      margin-left: 100px;
      margin-top: 0.5em; 
      margin-bottom: 0.8em; 
	  
     form,div {
	display: inline;
     }
   }
这样添加了form,并inline显示。

Line_item的Controller

编辑app/controllers/line_items_controller.rb文件:
  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/controllers/line_items_controller_test.rb文件:
  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

Cart的view

编辑app/views/carts/show.html.erb文件:
<% 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 %>


你可能感兴趣的:(Rails读书笔记第九章)