Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/53.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 嵌套形式/批量分配,验证麻烦,已通过多次_Ruby On Rails_Validation_Nested Forms_Has Many Through_Mass Assignment - Fatal编程技术网

Ruby on rails 嵌套形式/批量分配,验证麻烦,已通过多次

Ruby on rails 嵌套形式/批量分配,验证麻烦,已通过多次,ruby-on-rails,validation,nested-forms,has-many-through,mass-assignment,Ruby On Rails,Validation,Nested Forms,Has Many Through,Mass Assignment,好吧,所以我很难理解我哪里出了问题。我的关联设置正确,新订单工作正常(注意:如果两个字段都已填写)。如果我只填写一个字段,对于一项订单,批量分配将失败。我希望最终能够有3个字段,但如果只填写了其中的1或2个字段,一切都会顺利进行,空字段将被忽略。我花了几个小时试图理解这一点,现在我想尽一切办法来调试/理解这件事 视图/表单:new.html.erb <%= form_for @order do |f| %> <%= f.hidden_field :date, :value

好吧,所以我很难理解我哪里出了问题。我的关联设置正确,新订单工作正常(注意:如果两个字段都已填写)。如果我只填写一个字段,对于一项订单,批量分配将失败。我希望最终能够有3个字段,但如果只填写了其中的1或2个字段,一切都会顺利进行,空字段将被忽略。我花了几个小时试图理解这一点,现在我想尽一切办法来调试/理解这件事

视图/表单:new.html.erb

<%= form_for @order do |f| %>
  <%= f.hidden_field :date, :value => Time.now %>
  <%= f.fields_for :order_items do |f| %>
    <%= f.collection_select :item_id, Item.all, :id, :item_type, :include_blank => true %>
    <%= f.label "Quantity: "%>
    <%= f.text_field :quantity %><br>
  <% end %>
  <%= f.submit %>
<% end %>
型号:

class Order < ApplicationRecord
  has_many :order_items
  has_many :items, through: :order_items
  belongs_to :account

  def order_items_attributes=(order_items_attributes)
    order_items_attributes.each do |i, order_item_attributes|
      #binding.pry
      self.order_items.build(order_item_attributes)
    end
  end
end

class OrderItem < ApplicationRecord
  belongs_to :order
  belongs_to :item

  **NOTE: ALL THESE VALIDATIONS SEEM TO BE USELESS**
  #validates :item_id, :presence => {:message => "You must choose"}
  #validates :quantity, :presence => {:message => "You must choose"}
  #validates :order_id, :presence => {:message => "You must choose"}
  #validates_presence_of :item_id, :message => "You must choose an item"
  #validates_presence_of :quantity, :message => "You must enter the quantity"
end

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable

  has_one :account
  has_many :orders, through: :account
end

class Account < ApplicationRecord
  belongs_to :user
  has_many :orders, dependent: :destroy
end

class Item < ApplicationRecord
  #belongs_to :order
  has_many :order_items
end
类顺序{:message=>“您必须选择”}
#验证:数量,:状态=>{:消息=>“您必须选择”}
#验证:order_id,:presence=>{:message=>“您必须选择”}
#验证是否存在:item\u id,:message=>“您必须选择一个项目”
#验证是否存在:数量,:message=>“您必须输入数量”
结束
类用户<应用程序记录
#包括默认设计模块。其他可供选择的项目包括:
#:可确认、:可锁定、:超时和:全授权
设计:数据库可验证,可注册,
:可恢复,:可记忆,:可跟踪,:可验证
你有一个帐户吗
有很多订单,通过::帐户
结束
类帐户<应用程序记录
属于:用户
有很多:订单,依赖::销毁
结束
类项<应用程序记录
#属于:秩序
有很多:订购物品
结束
我希望一切都清楚了。我还是一个初学者,所以任何帮助都将不胜感激!谢谢

class Order < ApplicationRecord
  has_many :order_items
  has_many :items, through: :order_items
  belongs_to :account

  def order_items_attributes=(order_items_attributes)
    order_items_attributes.each do |i, order_item_attributes|
      #binding.pry
      self.order_items.build(order_item_attributes)
    end
  end
end

class OrderItem < ApplicationRecord
  belongs_to :order
  belongs_to :item

  **NOTE: ALL THESE VALIDATIONS SEEM TO BE USELESS**
  #validates :item_id, :presence => {:message => "You must choose"}
  #validates :quantity, :presence => {:message => "You must choose"}
  #validates :order_id, :presence => {:message => "You must choose"}
  #validates_presence_of :item_id, :message => "You must choose an item"
  #validates_presence_of :quantity, :message => "You must enter the quantity"
end

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable

  has_one :account
  has_many :orders, through: :account
end

class Account < ApplicationRecord
  belongs_to :user
  has_many :orders, dependent: :destroy
end

class Item < ApplicationRecord
  #belongs_to :order
  has_many :order_items
end