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 %>
<p id="notice"><%= notice %></p>
<% end %>

<h1>Your Pragmatic Catalog</h1>

<% @products.each do |product| %>
  <div class="entry">
    <%= image_tag(product.image_url) %>
    <h3><%= product.title %></h3>
    <p><%= sanitize(product.description) %></p>
    <div class="price_line">
      <span class="price"><%= number_to_currency(product.price) %></span>
      <%= button_to 'Add to Cart', line_items_path(product_id: product) %>
    </div>
  </div>
<% 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])  # <span style="font-family: Arial, Helvetica, sans-serif;">parmas是从browser URL request中获取的参数,</span><span style="font-family: Arial, Helvetica, sans-serif;">根据product的id,找到当前选择的product。</span>
    @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 %>
<p id="notice"><%= notice %></p>
<% end %>

<h2>Your Pragmatic Cart</h2>
<ul>    
  <% @cart.line_items.each do |item| %>
    <li><%= item.product.title %></li>
  <% end %>
</ul>
<%= link_to 'Edit', edit_cart_path(@cart) %> |
<%= link_to 'Back', carts_path %>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值