如何使用属于product_category的选项设置订购单

问题描述 投票:0回答:1

目标

我想设置一个订购单,用户可以在其中订购一种产品。填写完product_category之后,用户可以选择

  • 属于product_category的产品
  • 属于product_category的每个选项的数量。

当前状态

[我当前设置代码的方式在必须重新构建表单时会引起问题:

  • 当触发验证问题时,(1)product_category,(2)product和(3)选项为空,但保留仍然保存,从而导致保留两次保存的情况。

=>我知道是因为我先将保留内容保存在控制器中,然后再保存选项,但是我不知道如何解决此问题(例如,在触发验证以及用户填写时保存了保存内容然后正确地填写表格)。

代码

型号

class Order < ApplicationRecord
  belongs_to :store
  belongs_to :product

  has_many :order_options, dependent: :destroy
  has_many :options, through: :order_options
  accepts_nested_attributes_for :order_options
end

class OrderOption < ApplicationRecord
  belongs_to :option
  belongs_to :order
  accepts_nested_attributes_for :option
end

class Option < ApplicationRecord
  belongs_to :product_category
  has_many :order_options, dependent: :destroy
  has_many :orders, through: :order_options
end

class ProductCategory < ApplicationRecord
  belongs_to :store
  has_many :products, dependent: :destroy
  accepts_nested_attributes_for :products, allow_destroy: true
  has_many :options, dependent: :destroy
  accepts_nested_attributes_for :options, allow_destroy: true
end

order_controller

class OrdersController < ApplicationController
  # skip_before_action :authenticate_user!
  def new
    @user = current_user
    @store = Store.find(params[:store_id])
    @order = Order.new
    @order.build_order_contact
    @product_category_list = @store.product_categories
    @all_options = @store.options
    @products = []
    @options = []
    if params[:product_category].present?
      @products = ProductCategory.find(params[:product_category]).products
      @options = ProductCategory.find(params[:product_category]).options
    else
    end
    if request.xhr?
      respond_to do |format|
        format.json {
        render json: {products: @products, options: @options}
      }
        format.js
      end
    end

    authorize @order
  end

  def create
    @user = current_user
    @store = Store.find(params[:store_id])
    @order = Order.new(order_params)
    @order.store = @store
    authorize @order
    if @order.save
      params[:order_options_attributes].each do |order_option|
        if @option = Option.find_by(id: order_option[:option_id])
          @option_quantity = order_option[:option_quantity]
          @order.options << @option
          order_option = @order.order_options.where(option: @option)
          order_option.update(option_quantity: @option_quantity)
        end
      end
      redirect_to store_path(@store)
    else
      @product_category_list = @store.product_categories
      render 'new'
    end
  end

views / orders / new.js

$("#product_options").html("<%= escape_javascript(render partial: 'option_fields', collection: @options) %>");


$("#dynamic-products").empty();
<% @products.each do |pro| %>
    $("#dynamic-products").append('<option value="<%= pro.id %>"><%= pro.name %></option>')
<% end %>

views / orders / new.html.erb

<%= simple_form_for [@store, @order] do |f|%>
  <%= f.simple_fields_for :products do |product| %>
    <%= product.input :product_category, collection: @product_category_list, prompt: "Select type of product", label:false,
      input_html:{
      id: "product_category"
    }%>

  <%= f.association :product, collection: @products,  input_html:{
    value: @products.object_id,
    id: "dynamic-products"
  } %>

  <div class="product_category-options" id="product_options">
  </div>
  <% end %>
<% end %>

<script >
  // dynamic products and options for change category
  $(document).on("change", "#product_category", function(){
    var product_category = $(this).val();


    $.ajax({
      url: "/stores/<%= @store.id %>/orders/new",
      method: "GET",
      // dataType: "json",
      dataType: "script",
      data: {product_category: product_category},
      error: function (xhr, status, error) {
        console.error('AJAX Error: ' + status + error);
      },
      success: function (response) {
    }
  });
  });
  // dynamic products and option for releading form (e.g. new)
   $(document).ready(function(){
    var product_category = $("#product_category").val();

    $.ajax({
      url: "/stores/<%= @store.id %>/orders/new",
      method: "GET",
      dataType: "json",
      data: {product_category: product_category},
      error: function (xhr, status, error) {
        console.error('AJAX Error: ' + status + error);
      },
      success: function (response) {
    }
  });
  });

</script>

views / orders / _option_fields.html.erb

<div class="product_option order-form-quantity-row border-bottom col col-sm-10">
  <div class="product_option_name order-form-quantity-name">
    <strong>  <%= option_fields.name %></strong>
  </div>

  <div class="order-form-input">
    <%= hidden_field_tag("order_options_attributes[]option_id", option_fields.id ) %>
    <%= select_tag("order_options_attributes[]option_quantity", options_for_select((0..9)), {class:'form-control col col-sm-12'} ) %>

  </div>
</div>
javascript jquery ruby-on-rails simple-form
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.