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 设计实做。