美文网首页Ruby
购物车明细:数量更改

购物车明细:数量更改

作者: Sarah_友妹 | 来源:发表于2017-10-04 23:45 被阅读20次

Object 今天学的记得什么?

购物车明细里,商品数量可以更改,且不能超过库存数量。

Step 1: app/views/carts/index.html.erb中,增加以下code
<%= form_for cart_item, url: cart_item_path(cart_item) do %>
  <%= f.select :quantity, 1..cart_item.product.quantity %>
  <%= f.submit "更新", data: {disable_with: "submitting..."} %>
<% end %>
Step 2: cart_items controller里,修改update method
def update
  @cart_item = current_cart.cart_items.find(params[:id])
  if @cart_item.product.quantity >= cart_item_params[:quantity]
    @cart_item.update(cart_item_params)
    flash[:notice] = "成功更新"
  else
    flash[:warning] = "商品数量不足以加入购物车"
  end
end

Reflection 今天情绪的高点和低点是什么?

高点:中秋节,这个让人联想起家人团聚的日子。晚上,家人给我打来电话,聊了些家常,知道他们在一起过节,听到他们声音和笑声,感觉到温暖、开心。

低点:临近中午12点,肚子饿了,精神难集中,易烦躁,感觉不太好。不适合在这种状态下做耗神的活,比如学习新东西、打coding等。

Interpretation 今天一个重要的领悟是什么?

非嵌套的view里,针对object做form_for时,无需专门指明url。因为routing、controller、view三者一致,可以直接找到。

嵌套的view里,针对object做form_for时,则需专门指明url。比如文章开头的例子:
<%= form_for cart_item, url: cart_item_path(cart_item) do %>

Decision 明天要做什么?

按照精进练习的schedule, 做web API 设计实做。

相关文章

网友评论

  • sundar:请问楼主有什么好的学习资料分享下吗?
    Sarah_友妹:@sundar 付费课程,1万多的,目前暂时不开班,https://fullstack.xinshengdaxue.com/
    sundar:网上的?有没有链接:smile:
    Sarah_友妹:@sundar 我是报了网上一个全栈营班的,其它主要google

本文标题:购物车明细:数量更改

本文链接:https://www.haomeiwen.com/subject/zrchyxtx.html