Ruby on rails 如何创建和提交一个嵌套表单,其中的值通过关联从一个has\u many中填充?
我想创建一个Ruby on rails 如何创建和提交一个嵌套表单,其中的值通过关联从一个has\u many中填充?,ruby-on-rails,Ruby On Rails,我想创建一个winnow\u批次,它有许多bean\u装运通过bean\u winnow\u批次 create_table "bean_shipments", force: :cascade do |t| t.string "lotcode", null: false t.decimal "weight_remaining_kg" end create_table "bean_winnow_batc
winnow\u批次
,它有许多bean\u装运
通过bean\u winnow\u批次
create_table "bean_shipments", force: :cascade do |t|
t.string "lotcode", null: false
t.decimal "weight_remaining_kg"
end
create_table "bean_winnow_batches", force: :cascade do |t|
t.integer "bean_shipment_id"
t.integer "winnow_batch_id"
t.decimal "bean_shipment_weight_used_kg"
end
class BeanShipment < ApplicationRecord
has_many :bean_winnow_batches
has_many :winnow_batches, through: :bean_winnow_batches
accepts_nested_attributes_for :bean_winnow_batches
end
class WinnowBatch < ApplicationRecord
has_many :bean_winnow_batches
has_many :bean_shipments, through: :bean_winnow_batches
accepts_nested_attributes_for :bean_winnow_batches
end
class BeanWinnowBatch < ApplicationRecord
belongs_to :bean_shipment
belongs_to :winnow_batch
end
winnow_batches_controller.rb
def new
@winnow_batch = WinnowBatch.new
@shipment_options = BeanShipment.where("weight_remaining_kg > ?", 0)
@shipment_options.each do |ship|
@winnow_batch.bean_winnow_batches.build(bean_shipment_id: ship.id)
end
end
加载
winnow\u batch/new
view“未定义的方法'bean\u shipping'for#时收到的错误消息,用于解决您的错误问题:“未定义的方法‘bean_Shipping’用于#你是救命恩人!非常感谢。
def new
@winnow_batch = WinnowBatch.new
@shipment_options = BeanShipment.where("weight_remaining_kg > ?", 0)
@shipment_options.each do |ship|
@winnow_batch.bean_winnow_batches.build(bean_shipment_id: ship.id)
end
end