Ruby on rails 使用不同数量的产品创建订单
我有一个页面列出了可用的产品,我希望用户能够指出他想要的每种产品的数量。我不确定创建表单的正确方法是什么。我已经这样做了,但我相信这不是最好的方法Ruby on rails 使用不同数量的产品创建订单,ruby-on-rails,ruby,Ruby On Rails,Ruby,我有一个页面列出了可用的产品,我希望用户能够指出他想要的每种产品的数量。我不确定创建表单的正确方法是什么。我已经这样做了,但我相信这不是最好的方法 :post do%> 我的控制器里有 @products=[] 参数[:ads]。每个都做| ads| 如果产品[1][:数量].到\u i>0 @产品产品型号 class Product < ApplicationRecord has_and_belongs_to_many :orders, join_table: PurchasePr
:post do%>
我的控制器里有
@products=[]
参数[:ads]。每个都做| ads|
如果产品[1][:数量].到\u i>0
@产品产品型号
class Product < ApplicationRecord
has_and_belongs_to_many :orders, join_table: PurchaseProduct.table_name
has_many :purchase_products
end
类产品
订单模型
class Order < ApplicationRecord
has_and_belongs_to_many :products, join_table: PurchaseProduct.table_name
has_many :purchase_products
accepts_nested_attributes_for :purchase_products
end
类顺序
采购产品模型
class PurchaseProduct < ApplicationRecord
belongs_to :product
belongs_to :order
validates :quantity, length: { minimum: 1 }
end
class PurchaseProduct
订单
...
<%= form.fields_for :purchase_products do |another_form| %>
<div class="field">
<table>
<tr>
<td>
<%= another_form.label :product %>
<%= another_form.select(:product_id, Product.all.collect {|p| [ p.name, p.id ] }, ) %>
</td>
<td>
<%= another_form.label :quantity %>
<%= another_form.number_field :quantity %>
</td>
</tr>
</table>
</div>
<% end %>
...
。。。
...
对于渲染x倍产品和数量x.times{@order.purchase\u products.build}
查看构建此表单的完整步骤您能显示此表单创建的HTML表单吗?您是在问表单是否更好,还是有更好的方法来存储数据?