带有数量的表单似乎未提交

发布于 2024-09-06 08:17:56 字数 1020 浏览 5 评论 0原文

嘿伙计们,我一直在尝试理解文档并找到示例,但我不知所措。

这只是购物车内用于更新数量的提交表单。然而,更新的数量没有保存到数据库中——它总是使数量为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

断念 2024-09-13 08:17:56

首先,您需要将视图更改为如下所示:

<% form_for :lineitems, :url => {:controller => "line_items", :action => "cart_update" do |l| %>
  <% @cart.line_items.each_with_index do |line_item, index| %>
    <% fields_for "@cart.line_items[#{index}]", line_item do |i| %>
      <%= i.text_field :quantity, :size => '3', :value => line_item.quantity %>
    <% end %>
  <% end %>
<%= l.submit 'cart_update' %>

For a start you need to change the view to something like this:

<% form_for :lineitems, :url => {:controller => "line_items", :action => "cart_update" do |l| %>
  <% @cart.line_items.each_with_index do |line_item, index| %>
    <% fields_for "@cart.line_items[#{index}]", line_item do |i| %>
      <%= i.text_field :quantity, :size => '3', :value => line_item.quantity %>
    <% end %>
  <% end %>
<%= l.submit 'cart_update' %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文