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

此外,没有该行它可以工作,但删除链接不做任何事情:

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
4

3 回答 3

3

我陷入了同样的“未定义方法”错误,在我将f.fields_for调用的第一个参数更改为复数形式后一切正常,就像has_many关联一样。所以在你的情况下应该是:

= f.fields_for :invoice_line_items do |line_item|

因为 Invoice 模型中的关联是has_many :invoice_line_items

希望它可以帮助某人。

于 2015-03-10T23:11:52.417 回答
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 on

@fields[fields_blueprint_id]

根据您的错误消息,这显然是零。我对 Ryan 的代码感到迷茫。我看不出这个值是如何设置的,所以我无法帮助你弄清楚为什么这个值是 nil。但如果我不得不猜测,那是因为你没有添加

attr_accessible :invoice_line_items_attributes

到您的发票模型

于 2013-04-04T07:12:30.977 回答
0

这就是我想出的。如果关系的嵌套侧没有项目 - 比如说,如果这本书没有作者 - 我得到了错误。当我@book在控制器中加载时,我尝试检查authors数组是否为空,如果是,则添加一个新的空数组Author。当我这样做时,嵌套表单从未看到空关系,并且错误消失了。

我怀疑这是一个陷阱,nested_form如果有人想这样做并提交拉取请求,可以将其编码出来。

于 2013-06-15T00:53:53.427 回答