gpt4 book ai didi

ruby-on-rails - 默认更新 nested_attributes

转载 作者:太空宇宙 更新时间:2023-11-03 16:16:50 26 4
gpt4 key购买 nike

我尝试更新 iproduction,一个来自生产的嵌套形式,但我在这行出现了参数错误:“参数数量错误(0 代表 1)”

@production.update.iproductions_attributes(cow_id: @cow)

我的创作 Action 制作:

def create
@production = @ranch.productions.create(production_params)
@production.update(date: Date.today)
@cows = @ranch.cows
@cow = Cow.find_by(id: params[:id])
@production.update.iproductions_attributes(cow_id: @cow)
respond_to do |format|
if @production.save
format.html { redirect_to ranch_production_path(@production.ranch_id, @production), notice: 'Production was successfully created.' }
format.json { render :show, status: :created, location: @production }
else
format.html { render :new }
format.json { render json: @production.errors, status: :unprocessable_entity }
end
end
end

你能帮我解释一下语法吗?

最佳答案

应该是@production.iproductions.update_attributes(cow_id: @cow)

   def create
@production = @ranch.productions.create(production_params)
@production.update(date: Date.today)
@cows = @ranch.cows
@cow = Cow.find_by(id: params[:id])
@production.iproductions.update_attributes(cow_id: @cow)
respond_to do |format|
if @production.save
format.html { redirect_to ranch_production_path(@production.ranch_id, @production), notice: 'Production was successfully created.' }
format.json { render :show, status: :created, location: @production }
else
format.html { render :new }
format.json { render json: @production.errors, status: :unprocessable_entity }
end
end
end

关于ruby-on-rails - 默认更新 nested_attributes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42056016/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com