默认情况下更新nested_attributes

时间:2017-02-05 19:04:17

标签: ruby-on-rails ruby nested-attributes cocoon-gem

我尝试更新iproduction,一个来自生产的嵌套表单,我在这行中有一个params的错误: "错误的参数数量(0表示1)"

@ production.update.iproductions_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.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

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

1 个答案:

答案 0 :(得分:1)

应为@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