Update nested_attributes by default

China☆狼群 提交于 2019-12-11 01:25:27

问题


I try to update iproduction, a nested form from production and I have an error of params with this line : "wrong number of arguments (0 for 1)"

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

My create action production:

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

Can you help me about the syntax ?


回答1:


It should be @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


来源:https://stackoverflow.com/questions/42056016/update-nested-attributes-by-default

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!