Rails控制器重定向不起作用

时间:2014-12-15 18:15:51

标签: ruby-on-rails

我在Rails控制器中有代码:

  def addinvtimes
    @invoice = params[:invtimes][:invoice_id]
    if params[:event_ids] != nil
      params[:event_ids].each do |i|
        newinvtime = Invtime.new(
            linetype_id: 1,
            invoice_id: @invoice,
            event_id: i.to_i
        )
        if newinvtime.save
          format.html { redirect_to invoice_path(@invoice), :notice => 'Invoice Time was successfully added.' }
        else
          format.html { redirect_to invoice_path(@invoice), :notice => 'ERROR.' }
        end
      end
    end
  end

为什么我在这里收到错误too few arguments

format.html { redirect_to invoice_path(@invoice), :notice => 'Invoice Time was successfully added.' }

在控制台中,我得到了

>> invoice_path(@invoice)
=> "/invoices/29"

感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

您忘记将format.部分包裹在respond_to块中。应该是这样的:

respond_to do |format|
  if newinvtime.save
      format.html { redirect_to invoice_path(@invoice), :notice => 'Invoice Time was successfully added.' }
  else
      format.html { redirect_to invoice_path(@invoice), :notice => 'ERROR.' }
  end
end