1
後消失我有一個模型:的Rails:嵌套模型的驗證
class Invoice < ActiveRecord::Base
belongs_to :case
has_many :invoice_positions
accepts_nested_attributes_for :invoice_positions, allow_destroy: true
,我有一個形式createn與https://github.com/nathanvda/cocoon,但我有一個小問題 - 驗證失敗,所有invoice_positions消失後。
這裏是形式,其中i渲染位置的一部分:
<% f.fields_for :invoice_positions do |invoice_position| %>
<%= render 'invoice_position_fields', f: invoice_position %>
<% end %>
和我的控制器創建行動:從控制檯
def create
@case = Case.find(params[:case_id])
@invoice = Invoice.new(invoice_params)
@invoice.case = @case
if @invoice.save
flash[:success] = 'Faktura zapisana'
redirect_to case_case_invoices_path(@case)
else
@invoice.invoice_positions.build(params[:invoice_positions_attributes])
render 'new'
end
end
下面是參數:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"f1IUw+mt10QX3PFy4g1Hv+8VkD8sQR7roPKrh+6N4Va6Un2mGrR86/XR91hsRjI4V6FLNyavyc8TeGZt9E2RoQ==", "invoice"=>{"date(1i)"=>"2016", "date(2i)"=>"1", "date(3i)"=>"26", "pay_date(1i)"=>"2016", "pay_date(2i)"=>"2", "pay_date(3i)"=>"5", "city"=>"Rzeszów", "person"=>"Test Test", "comment"=>"", "invoice_positions_attributes"=>{"1453833757786"=>{"name"=>"dwqdwqdwq", "kind"=>"", "price"=>"", "quantity"=>"", "tax"=>"", "_destroy"=>"false"}, "1453833759600"=>{"name"=>"wdqdwqqwd", "kind"=>"", "price"=>"", "quantity"=>"", "tax"=>"", "_destroy"=>"false"}}}, "case_id"=>"1"}
這裏invoice_position_fields視圖:
<tr class="nested-fields">
<td>
<%= f.text_field :name, class: 'form-control', placeholder: 'Nazwa' %>
</td>
<td>
<%= f.text_field :kind, class: 'form-control', placeholder: 'Typ (np. usł., szt.)' %>
</td>
<td>
<%= f.text_field :price, class: 'form-control', placeholder: 'Cena jedn. netto' %>
</td>
<td>
<%= f.text_field :quantity, class: 'form-control', placeholder: 'Ilość' %>
</td>
<td>
<%= f.text_field :tax, class: 'form-control', placeholder: 'VAT' %>
</td>
<td></td>
<td>
<%= link_to_remove_association icon('trash'), f, {class: 'btn btn-danger'} %>
</td>
</tr>
你能張貼在invoice_position_fields模板代碼 – Swards
'@ invoice.invoice_positions.build(PARAMS [:發票] [:invoice_positions_attributes])'這將幫助。請提供從控制檯傳遞給創建方法的參數列表。 – user100693
@ShefaleeChaudhary它仍然不起作用 – rafal235