[英]How to link options to orders incl. the quantity of options?
How can I include product options to an order incl.如何在订单中包含产品选项,包括。 the quantities for the respective options?
各个选项的数量?
--> It is unclear to me how to deal with the many to many relationship between orders and product options and linking them all together in an order form. --> 我不清楚如何处理订单和产品选项之间的多对多关系,并将它们全部链接到一个订单表格中。
Please find below some code on how my tables are linked at the moment and how I currently try to solve it in a form.请在下面找到一些关于我的表格目前如何链接以及我目前如何尝试以表格形式解决它的代码。
models楷模
class Order < ApplicationRecord
has_many :order_options, dependent: :destroy
end
class OrderOption < ApplicationRecord
belongs_to :option
belongs_to :order
end
class Option < ApplicationRecord
belongs_to :product_type
has_many :order_options, dependent: :destroy
end
class ProductType < ApplicationRecord
has_many :options, dependent: :destroy
end
orders_controller订单控制器
class OrdersController < ApplicationController
def new
@shop = Shop.find(params[:shop_id])
@order = Order.new
@orders = @shop.orders
@order.order_products.build
@order.order_options.build
@product_type_list = @shop.product_types
@order.build_order_contact
@products = []
@options = []
# Display products/options for type
if params[:product_type].present?
@products = ProductType.find(params[:product_type]).products
@options = ProductType.find(params[:product_type]).options
end
if request.xhr?
respond_to do |format|
format.json {
render json: {products: @products, options: @options}
}
end
end
order form订单
<%= simple_form_for [@shop, @order] do |f|%>
<%= f.simple_fields_for :order_products do |order_product| %>
#select product for which options are shown -->
<%= order_product.simple_fields_for :products do |product| %>
<%= product.input :product_type_id, collection: @product_type_list,
input_html:{
value: @product_type_list.object_id,
id: "product_type"
}
%>
<% end %>
#area to display options belonging to the product
chosen above incl. dropdown field where users
can select a quantity. -->
<h4>Options:</h4>
<div id="render-options">
# Place where Javascript and Ajax can be rendered with possible options and dropdown field for quantity
</div>
<%= f.button :submit%>
Javascript/Ajax Javascript/Ajax
<script >
// Grab selected product_type on which options are based -->
$(document).on("change", "#product_type", function(){
var product_type = $(this).val();
$.ajax({
url: "/shops/<%= @shop.id %>/orders/new",
method: "GET",
dataType: "json",
data: {product_type: product_type},
error: function (xhr, status, error) {
console.error('AJAX Error: ' + status + error);
},
success: function (response) {
console.log(response);
// dynamic rendered options -->
var options = response["options"];
$("#render-options").html("");
$("#render-options").append("");
for(var i=0; i< options.length; i++){
$("#render-options").append('<option value="' + options[i]["id"] + '">' + options[i]["name"] + '</option>');
console.log(options[i].orders)
}
}
});
});
</script>
For this example I'm going to go with a classic order form setup as its less confusing (at least to me).对于这个例子,我将使用经典的订单设置 go,因为它不那么令人困惑(至少对我而言)。
class Order < ApplicationRecord
has_many :line_items, dependent: :destroy
has_many :products, through: :line_items
accepts_nested_attributes_for :line_items
end
class LineItem
belongs_to :order
belongs_to :product
end
class OrdersController
def new
@order = Order.new
# seed the form with 5 empty lines
5.times { @order.line_options.new }
end
def create
@order = Order.new(order_params)
# ...
end
# ...
private
def order_params
params.require(:order).permit( line_items_attributes: [:product_id, :quantity] )
end
end
<%= simple_form_for [@order] do |f| %>
<%= f.simple_fields_for :line_items do |item| %>
<%= f.association :product %>
<%= f.input :quantity %>
<% end %>
<% end %>
This is basically the setup for a "classic" order form which emulates paper.这基本上是模拟纸张的“经典”订单的设置。 In reality this is not very useful unless your dealing with poor corporate minions that actually have to fill it out from top to bottom.
实际上,除非您与实际上必须从上到下填写它的可怜的公司奴才打交道,否则这并不是很有用。
For an actual web shop the users are actually clicking on forms from GET /products/id
that should actually be sending individual POST /orders/:order_id/line_item
requests to a nested route to "add items to the cart".对于实际的 web 商店,用户实际上是从
GET /products/id
中单击 forms ,这实际上应该将单个POST /orders/:order_id/line_item
请求发送到嵌套路由以“将项目添加到购物车”。 The order itself is often created implicitly.订单本身通常是隐式创建的。
In general the greatest misstake is thinking that you can treat placing an order as one single http call which creates the whole order and processes it.一般来说,最大的错误是认为您可以将下订单视为一个单独的 http 调用,该调用创建整个订单并对其进行处理。 Instead you need to treat it as a set of atomic operations that lead the user to eventually checking out.
相反,您需要将其视为一组引导用户最终签出的原子操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.