Ruby on rails 表单的嵌套字段\u不会创建子对象rails 5

Ruby on rails 表单的嵌套字段\u不会创建子对象rails 5,ruby-on-rails,ruby,ruby-on-rails-5,form-for,fields-for,Ruby On Rails,Ruby,Ruby On Rails 5,Form For,Fields For,sale.rb“家长” class Salelambda{a | a[:content].blank?}, :allow_destroy=>true 结束 类SalesController

sale.rb“家长”

class Salelambda{a | a[:content].blank?},
:allow_destroy=>true
结束
类SalesController<应用程序控制器
在执行以下操作之前:设置销售,仅:[:显示,:编辑,:更新,:销毁]
#获取/销售
#GET/sales.json
def索引
@销售=销售
结束
#获取/销售/1
#GET/sales/1.json
def秀
结束
#获取/销售/新
def新
@sale=sale.new
@sale.branch\u history\u solds.build
结束
#获取/销售/1/编辑
定义编辑
结束
#邮递/销售
#POST/sales.json
def创建
@销售=销售。创建(销售参数)
#@sale.branch\u history\u solds.build
回应待办事项|格式|
如果@sale.save
format.html{将_重定向到@sale,注意:sale已成功创建。}
format.json{render:show,status::created,location:@sale}
其他的
format.html{render:new}
format.json{render json:@sale.errors,status::unprocessable_entity}
结束
结束
结束
#修补/出售/销售/1
#PATCH/PUT/sales/1.json
def更新
@sale=sale.find(参数[:id])
回应待办事项|格式|
如果@sale.update_属性(sale_参数)
format.html{将_重定向到@sale,注意:sale已成功更新。}
format.json{render:show,status::ok,location:@sale}
其他的
format.html{render:edit}
format.json{render json:@sale.errors,status::unprocessable_entity}
结束
结束
结束
#删除/sales/1
#删除/sales/1.json
def销毁
@销毁
回应待办事项|格式|
format.html{重定向到sales_url,注意:“Sale已成功销毁”。}
format.json{head:no_content}
结束
结束
私有的
#使用回调在操作之间共享公共设置或约束。
def set_销售
@sale=sale.find(参数[:id])
结束
#永远不要相信来自恐怖网络的参数,只允许白名单通过。
def销售参数
参数require(:sale).permit(:收据号,:客户名,:电话号码,:电子邮件,:分支机构id,:支付,分支机构历史记录销售属性:[:id,:销售,:分支机构产品id])
结束
结束
branch_history_Seld.rb“Child”

class BranchHistorySold
最后是我的表单,其中包含属性的字段

<div class="container">
      <div class="row">
          <%= form_for @sale, html: { class: "form-horizontal" } do |f| %>
                <!-- Text input-->
                <div class="form-group">
                    <label class="col-md-1 control-label">R/NO</label>
                    <div class="col-md-6">
                        <%= f.collection_select(:branch_id, Branch.all, :id, :name) %>
                    </div>
                </div>
                <!-- Text input-->
                <div class="form-group">
                    <label class="col-md-1 control-label">R/NO</label>
                    <div class="col-md-6">
                        <%= f.text_field :receipt_no, placeholder: "Receipt number", class: "form-control input-md" %>
                    </div>
                </div>
                <!-- Text input-->
                <div class="form-group">
                    <label class="col-md-1 control-label" >Name</label>
                    <div class="col-md-6">
                        <%= f.text_field :customer_name, placeholder: "Prince Abalogu", class: "form-control input-md" %>
                    </div>
                </div>
                <!-- Appended Input-->
                <div class="form-group">
                    <label class="col-md-1 control-label">Number</label>
                    <div class="col-md-6">
                        <%= f.text_field :phone_number, placeholder: "08185438075", class: "form-control input-md" %>
                    </div>
                </div>
                <!-- Appended Input-->
                <div class="form-group">
                    <label class="col-md-1 control-label">E-mail</label>
                    <div class="col-md-6">
                        <%= f.text_field :email, placeholder: "example@yahoo.com", class: "form-control input-md" %>
                    </div>
                </div>

                <!-- Appended Input-->
                <%= f.fields_for :branch_history_solds, @sale.branch_history_solds.build do |b| %>
                  <div class="form-group">
                      <label class="col-md-1 control-label">Product</label>
                      <div class="col-md-6">
                        <%= b.number_field :sold, placeholder: "Quantity" %>
                      </div>
                  </div>
                    <div class="form-group">
                        <label class="col-md-1 control-label"></label>
                        <div class="col-md-6">
                            <% @branch = BranchProduct.where :branch_id, 19 %>
                            <%= b.collection_select(:branch_product_id, BranchProduct.where(branch_id: params[:branch_id] ), :id, :name ) %> Select Product
                        </div>
                    </div>
                <% end %>

                <!-- Appended Input-->
                <div class="form-group">
                    <label class="col-md-1 control-label"></label>
                    <div class="col-md-6">
                        <%= f.check_box :paid %> Paid
                    </div>
                </div>

                <!-- Button (Double) -->
                <div class="form-group">
                    <label class="col-md-1 control-label"></label>
                    <div class="col-md-8">
                        <%= f.button :submit %>
                    </div>
                </div>
          <% end %>
      </div>
    </div>

R/否
R/否
名称
数
电子邮件
产品
精选产品
支付

表单显示,但我现在唯一的问题是,在我提交后,它不会创建任何分支历史解决方案。问题是,您用于评估是否应拒绝嵌套记录的lambda将始终返回true,因为您的表单/模型没有
content
属性:

class Sale < ActiveRecord::Base
  has_many :branch_history_solds
  accepts_nested_attributes_for :branch_history_solds, :reject_if => lambda { |a| a[:content].blank? }, 
                                :allow_destroy => true
end  
然而,你的一般设置只是简单的奇怪,我不知道它是否只是命名,但它没有太多的意义

如果您想创建销售点系统或订单管理系统,您可以这样做:

class Order
  belongs_to :customer
  has_many :line_items
  has_many :products, through: :line_items
  accepts_nested_attributes_for :line_items,
    allow_destroy: true,
    reject_if: -> { |li| li[:product_id].blank? || li[:quantity].blank? }
end

# columns:
# - order_id [integer, index, foreign key]
# - product_id [integer, index, foreign key]
# - quantity [decimal or integer]
# - price [decimal]
# - subtotal [decimal]
class LineItem
  belongs_to :order
  belongs_to :product
end

class Product
  has_many :line_items
  has_many :orders, through: :line_items
end

请注意,您应该只允许用户通过数量非常有限的o
class Sale < ActiveRecord::Base
  has_many :branch_history_solds
  accepts_nested_attributes_for :branch_history_solds, :reject_if => lambda { |a| a[:content].blank? }, 
                                :allow_destroy => true
end  
class Sale < ActiveRecord::Base
  has_many :branch_history_solds
  accepts_nested_attributes_for :branch_history_solds, :reject_if => lambda { |a| a[:branch_product_id].blank? }, 
                                :allow_destroy => true
end  
class Order
  belongs_to :customer
  has_many :line_items
  has_many :products, through: :line_items
  accepts_nested_attributes_for :line_items,
    allow_destroy: true,
    reject_if: -> { |li| li[:product_id].blank? || li[:quantity].blank? }
end

# columns:
# - order_id [integer, index, foreign key]
# - product_id [integer, index, foreign key]
# - quantity [decimal or integer]
# - price [decimal]
# - subtotal [decimal]
class LineItem
  belongs_to :order
  belongs_to :product
end

class Product
  has_many :line_items
  has_many :orders, through: :line_items
end
class OrdersController

  def new
  end


  def create
    @order = Order.new(order_params) do
      order.customer = current_user
    end
    if (@order.save)

    else

    end
  end

  private
  def order_params
    params.require(:order)
          .permit(:foo, :bar, line_item_attributes: [:product_id, :quantity])
  end
end