Ruby on rails 如何使用Rails在一个表单中添加多个单名称元素?

Ruby on rails 如何使用Rails在一个表单中添加多个单名称元素?,ruby-on-rails,ruby,ruby-on-rails-4,Ruby On Rails,Ruby,Ruby On Rails 4,我有三个模型需要一起工作-product、line\u-product和order,其中line\u-product反映了订单的product。所以看起来是这样的: = form_for @line_product do |lp| = lp.hidden_field :order_id, value: Order.last.id %section#product_list - if notice %section.notice=notice %ul#produ

我有三个模型需要一起工作-
product
line\u-product
order
,其中
line\u-product
反映了
订单的
product
。所以看起来是这样的:

= 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'
product.rb

has_many :line_products
has_many :line_products
belongs_to :order
belongs_to :product
订单.rb

has_many :line_products
has_many :line_products
belongs_to :order
belongs_to :product
line\u product.rb

has_many :line_products
has_many :line_products
belongs_to :order
belongs_to :product
我想制作一个表单,其中有一个产品列表,每个产品旁边都有一个输入字段,用户可以为任何产品输入任何数字(计数),单击提交,所有计数超过0的产品都将成为
line\u products
for
order
其中
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表单post问题,用rails/haml生成HTML不是问题所在


使用数组表单

有很多方法可以处理此问题,例如,您可以使用
接受嵌套属性
,这是一种相当通用的生成表单的方法,可以创建/编辑多个对象,然后处理该数据

对于这种特殊情况,一个稍微简单的替代方法是为每个产品生成一个数字输入(而不是您拥有的一对隐藏输入和数字输入)

这将导致
params[:count]
成为表单的散列

{ "1" => 23, "2" => "", "3"=> "1"}
假设您有3个ID为1,2,3的产品,并且您为第一个数量输入了23,为最后一个数量输入了1

然后需要一些控制器代码来迭代这个散列,并构建相应的line_产品对象