带有数量的表单似乎未提交
嘿伙计们,我一直在尝试理解文档并找到示例,但我不知所措。
这只是购物车内用于更新数量的提交表单。然而,更新的数量没有保存到数据库中——它总是使数量为 0。请帮忙。
表单
<% for line_item in @cart.line_items %>
<% form_for :lineitems, :url => {:controller => "line_items", :action => "cart_update", :id => "#{line_item.product_id}"} do |l| %>
<%= l.text_field :quantity, :size => '3', :value => line_item.quantity %>
<%= l.submit 'cart_update' %>
<% end %>
路由
map.connect 'line_item_update', :controller => 'line_items', :action => 'cart_update'
控制器
def cart_update
@product = Product.find(params[:id])
item = LineItem.find_or_create_by_cart_id(:cart_id => current_cart.id, :product_id => @product.id, :quantity => 0, :unit_price => @product.price)
item.quantity = (params[:quantity]).to_i
item.save
redirect_to :controller => 'products'
end
Hey guys, I've been trying to understand the documentation and find an example, but I'm at a loss.
This is just a submit form within the cart for updating the quantity. However, the updated quantity is not getting saved to the database -- it always makes the quantity 0. Please help.
Form
<% for line_item in @cart.line_items %>
<% form_for :lineitems, :url => {:controller => "line_items", :action => "cart_update", :id => "#{line_item.product_id}"} do |l| %>
<%= l.text_field :quantity, :size => '3', :value => line_item.quantity %>
<%= l.submit 'cart_update' %>
<% end %>
Route
map.connect 'line_item_update', :controller => 'line_items', :action => 'cart_update'
Controller
def cart_update
@product = Product.find(params[:id])
item = LineItem.find_or_create_by_cart_id(:cart_id => current_cart.id, :product_id => @product.id, :quantity => 0, :unit_price => @product.price)
item.quantity = (params[:quantity]).to_i
item.save
redirect_to :controller => 'products'
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您需要将视图更改为如下所示:
For a start you need to change the view to something like this: