0
我有一個模態「A」,它有一個has_many關聯的line_item模型「B」。這意味着B與A相關我對於B驗證在驗證錯誤重置我的值爲什麼
validates_presence_f :B
validates_associated :B
現在在我的形式我曾用「fields_for」保存B的值A的模型,如果我提交一份空白表格比 驗證失敗並顯示有關訂單項B的出現的錯誤消息,但B的字段已禁用,我必須重新顯示其字段。任何人都能預測爲什麼會發生這種情況。
這裏是我的模型: A型:
class Purchase < ActiveRecord::Base
has_many :purchase_line_items
accepts_nested_attributes_for :purchase_line_items, :reject_if => lambda {|a| a[:account_id].blank? }, :allow_destroy =>true
validates_presence_of :purchase_line_items
validates_associated :purchase_line_items
end
和B型:
class PurchaseLineItem < ActiveRecord::Base
belongs_to :purchase
end
在我的控制器
:
class PurchasesController < ApplicationController
def new
@purchase = Purchase.new
@purchase.purchase_line_items.build
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @purchase }
end
end
end
去年我的觀點
和:
<%= form_for @purchase, :html => {:multipart => true} do |f| %>
<%= render 'shared/form_error', :object => @purchase %>
<% @purchase.purchase_line_items.each_with_index do |purchase_line_item, index| %>
<%= render "purchase_line_items", :purchase_line_item => purchase_line_item, :index => index %>
<% end %>
<%= f.submit %>
<% end %>
而且在行項目部分我有:
<tr id="row<%= index %>" valign="top" >
<%= hidden_field_tag "purchase[purchase_line_items_attributes][#{index}][id]",purchase_line_item.id%>
<td valign="top">
<%= select_tag "purchase[purchase_line_items_attributes][#{index}][account_id]", options_from_collection_for_select(@to_accounts, :id, :name,:selected => purchase_line_item.account_id), :include_blank => true, :class=>"full" %>
</td>
<td><%= text_field_tag "purchase[purchase_line_items_attributes][#{index}][amount]", purchase_line_item.amount, :class => 'full', :id => 'total', :readonly => 'readonly', :size => 5%></td>
<td><%= link_to image_tag("/images/black_icon/ic_cancel.png"),{:action => :remove_line_item, :index => index}, :remote => true unless index == 0 %></td>
</tr>
您是否錯誤地複製了'validates_presence_f'? 此外,您的控制器操作是什麼,在驗證失敗後如何重新渲染窗體視圖?需要更多信息來調試。 – nickpellant
是的,它是不正確的複製,我不明白在驗證失敗後重新呈現表單視圖,你能解釋一下嗎? – Ravindra
如果您將提供視圖代碼(表單邏輯所在的位置)和模型代碼,我們可以對其進行調試。 – thesis