gpt4 book ai didi

ruby-on-rails - 如何使用 Rails 以一种形式添加多个单名称元素?

转载 作者:数据小太阳 更新时间:2023-10-29 08:11:48 24 4
gpt4 key购买 nike

我有三个模型需要协同工作 - productline_productorder,其中 line_product 镜像产品 用于订单。所以它看起来像这样:

product.rb

has_many :line_products

order.rb

has_many :line_products

line_product.rb

belongs_to :order
belongs_to :product

我想制作一个表格,其中有一个产品列表,每个产品旁边都有一个输入字段,用户可以为任何产品输入任何数字(计数),单击提交,所有产品的计数都超过0 在它们的形式中,将成为 line_products for order where order.id: 1 (例如)。截至目前,只有最后一个产品被添加到订单中。

我该怎么做?

编辑

表单如下所示:

= form_for @line_product do |lp|
= lp.hidden_field :order_id, value: Order.last.id
%section#product_list
- if notice
%section.notice=notice
%ul#products
-@products.each do |p|
%li.product_list
%article.product
%section.product_left
= image_tag p.image.url(:medium)
%div.clear
%span.price= number_to_currency(p.price,:unit=>'€ ')

%section.product_right
%section.product_about
%span.title= p.title
%span.description= p.description
%span.description.desceng= p.description_eng
= lp.hidden_field :product_id, value: p.id

%section.product_order
= lp.number_field(:count, min: '0', value: '', class: 'product_count')


%section#order_accepting
= lp.submit "Add to cart", class:'add_to_cart'

最佳答案

您达到了预期的行为。这实际上是 Rails 表单处理未选中复选框的方式。这实际上更像是一个 HTTP 表单发布问题,使用 rails/haml 生成 HTML 不是问题。

使用数组形式POST an array from an HTML form without javascript

关于ruby-on-rails - 如何使用 Rails 以一种形式添加多个单名称元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31534558/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com