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

同じ「未定義のメソッド」エラーで立ち往生し、 has_many関連付けと同様に、f.fields_for呼び出しの最初のパラメーターを複数形に変更した後、すべて正常に機能しました。したがって、あなたの場合は次のようになります。

= f.fields_for :invoice_line_items do |line_item|

Invoice モデルの関連付けはhas_many :invoice_line_items

それが誰かを助けることを願っています。

于 2015-03-10T23:11:52.417 に答える
0

2 つの別々の問題があるようです。

1) リンクを削除しても何も起こりません。Rails のどのバージョンを使用していますか? アセット パイプラインは 3.1 まで追加されなかったので、それよりも低いものを使用している場合は、Github の「Non Asset Pipeline Setup」の下にある指示に従ってください。これで問題 1 が解決される可能性があります。

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 のコードを見て迷ってしまいました。この値がどのように設定されるのかわかりません。そのため、この値が 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 に答える