【发布时间】:2019-12-18 15:47:40
【问题描述】:
这是我的问题。我有两个模型(建筑和客户)
class Construction < ApplicationRecord
has_many :works
belongs_to :customer
end
class Customer < ApplicationRecord
has_many :constructions
end
我想在创建新结构时将客户与结构相关联。 为此,我必须遵循控制器的方法(这显然是错误的)
def create
# @construction = Construction.new(constructions_params) (commented)
@construction = Construction.new(customer: @customer)
@customer = Customer.find(params[:customer_id])
@construction.save!
end
从参数中我可以理解,构造没有保存,因为它没有附加到客户,因此无法创建。
我是 Rails 新手,我已经挣扎了好几个小时了..
希望有人能帮助我。
非常感谢
【问题讨论】:
-
params[:construction][:customer_id]下的 customer_id 可能吗?puts params.inspect并检查它 -
显示您的表格。执行
puts params.inspect并显示输出。
标签: ruby-on-rails ruby activerecord rails-activerecord model-associations