使用nested_form_for时出现"undefined method 'values_at' for nil:NilClass"错误

3

我正在使用ryanb的nested_form gem,但它似乎不能正常工作。删除链接无法正常工作(我正确安装了//= require jquery_nested_form,看起来它已经加载,但我一直收到这个错误:

undefined method `values_at' for nil:NilClass

当我进入添加页面时:
= f.link_to_add "Add a line item", :invoice_line_items

此外,如果没有那行代码也可以正常运行,但“remove”链接不起作用。
line_item.link_to_remove "Remove this line item"

这是我的代码:

.row-fluid
  .span10.offset1
    = nested_form_for(@invoice) do |f|
      - if @invoice.errors.any?
        #error_explanation
          %h2
            = pluralize(@invoice.errors.count, "error")
            prohibited this invoice from being saved:
          %ul
            - @invoice.errors.full_messages.each do |msg|
              %li= msg
      .fieldset
        %legend
          = "New Invoice for #{@client.name}"
        .form-horizontal
          .pull-left
            .control-group
              %label.control-label{:style => "width: 100px;"}
                Invoice ID
              .controls{:style => "margin-left: 120px;"}
                = f.text_field :client_invoice_id, :class => "input-small", :placeholder => @invoice_count_placeholder
            .control-group
              %label.control-label{:style => "width: 100px;"}
                Due Date
              .controls{:style => "margin-left: 120px;"}
                = f.select :payment_term, @payment_terms, { :required => "true" }, { :class => "span10" }
          .pull-right
            .control-group
              %label.control-label
                Issue Date
              .controls{:style => "margin-right: 60px;"}
                = f.text_field :issue_date, :id => "date-picker", :class => "input-small", :required => "true"
            .control-group
              %label.control-label
                Discount
              .controls{:style => "margin-right: 60px;"}
                .input-append
                  = f.text_field :discount, :class => "input-small", :placeholder => "Optional"
                  %span.add-on %
        .row-fluid
          %table.table
            = f.fields_for :invoice_line_item do |line_item|
              %tr
                %th
                %th.span8 Description
                %th.span1 Quantity
                %th.span1 Rate
                %th.span1 Amount
              %tr
                %td= line_item.link_to_remove "Remove this line item"
                %td= line_item.text_field :description
                / %td= text_area_tag 'body', nil, :style => "width:96%;"
                %td= text_field_tag 'hello', nil, :class => "input-mini"
                %td= text_field_tag 'hello', nil, :class => "input-mini"
                %td $99.99

            = f.link_to_add "Add a line item", :invoice_line_items
        .form-actions
          = f.submit "Preview Invoice", :class => "btn btn-primary pull-right"

你有什么思路是错的吗?我想轻松地添加发票行项目,然后保存整个发票。以下是我的关联:

class Invoice < ActiveRecord::Base
  ## ASSOCIATIONS ##
  belongs_to :user
  belongs_to :client
  has_many :invoice_line_items
  ## NESTED ATTRIBUTES ##
  accepts_nested_attributes_for :invoice_line_items, :allow_destroy => true

class InvoiceLineItem < ActiveRecord::Base
  ## ASSOCIATIONS ##
  belongs_to :invoice

编辑:这是我的发票控制器新操作:

def new
    @client = current_user.clients.find(params[:client_id])
    @invoice = Invoice.new(:client_id => @client.id)
    @payment_terms = Invoice.payment_terms

    if @client.invoices.count > 0
      @invoice_count_placeholder = "Last used: #{@client.invoices.last.client_invoice_id}"
    else
      @invoice_count_placeholder = ""
    end

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @invoice }
    end
  end

我也遇到了同样的问题。我有另一个模型,它可以正常工作...但是我现在想要设置的那个不行。 - pjmorse
我发现:invoice_line_itemsfields_forlink_to_add中都必须是复数。 - Abhi
3个回答

3

我曾经遇到相同的“undefined method”错误,之后我将f.fields_for调用的第一个参数改为复数形式后一切正常运行,就像has_many关联一样。所以在您的情况下应该是:

= f.fields_for :invoice_line_items do |line_item|

由于发票模型中的关联是 has_many :invoice_line_items

希望这能帮助到某些人。


0

看起来你有两个不同的问题。

1)删除链接没有任何作用。你使用的Rails版本是什么?资产管道直到3.1才被添加,因此如果您使用的是低版本,则可以按照Github上“非资产管道设置”下的说明解决第一个问题。

2)使用link_to_add助手会导致错误。我在这里查看了代码 https://github.com/ryanb/nested_form/blob/master/lib/nested_form/builder_mixin.rb

该方法在...上调用values_at

@fields[fields_blueprint_id]

根据您的错误信息,该值显然为空。我看了一下Ryan的代码,感到有些迷失。我不知道这个值是如何被设置的,所以我不能提供太多帮助来弄清为什么这个值为空。但如果我必须猜的话,那可能是因为您没有添加


attr_accessible :invoice_line_items_attributes

到你的发票模型


使用Rails 3.2时,我遇到了这个问题,并且attr_accessible已经正确设置。感谢您指出问题的源头,不过这很奇怪... - pjmorse

0

这是我想到的解决方法。如果关系中的嵌套侧没有任何项目 - 例如书籍没有作者 - 我会得到错误信息。在控制器中加载 @book时,尝试检查是否 authors 数组为空,并添加一个新的、空的 Author。当我这样做时,嵌套表单从未看到空关系,并且错误消失了。

我猜这是一个容易被编码出来的问题,如果有人想要这样做并提交拉取请求,就可以解决它。


请把以下与程序设计有关的内容从英语翻译成中文。只需返回翻译后的文本:注:“书”和“作者”这个例子是为了回答方便而想象的。 - pjmorse

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接