我正在使用ryanb的nested_form gem,它似乎沒有正常工作。刪除鏈接無法正常工作(我安裝正確// =需要jquery_nested_form,它看起來像它的加載,但我不斷收到此錯誤:使用nested_form_for獲取未定義的方法`values_at'for nil:NilClass使用nested_form_for
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
我有這個相同的問題。我有另外一種模式,在這種模式下工作...但我現在試圖設置的模式不適用。 – pjmorse
我發現':invoice_line_items'必須是** fields_for **和** link_to_add **中的複數** – Abhi