2012-05-23 43 views
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> 
+0

您是否錯誤地複製了'validates_presence_f'? 此外,您的控制器操作是什麼,在驗證失敗後如何重新渲染窗體視圖?需要更多信息來調試。 – nickpellant

+0

是的,它是不正確的複製,我不明白在驗證失敗後重新呈現表單視圖,你能解釋一下嗎? – Ravindra

+0

如果您將提供視圖代碼(表單邏輯所在的位置)和模型代碼,我們可以對其進行調試。 – thesis

回答

0

如果我理解正確的,你有嵌套形式問題。

你有什麼理由readonly財產:amount領域?

我不知道,爲什麼你沒有使用fields_for的方法,但在這種情況下,你不能驗證沒有它的嵌套字段。因此,您的代碼必須是這樣的:

<%= form_for @purchase, :html => {:multipart => true} do |f| %> 
    <%= render 'shared/form_error', :object => @purchase %> 

    <%= f.fields_for :purchase_line_items do |pl| %> 
     <tr id="row<%= pl.object.id %>" valign="top" > 
      <%= pl.hidden_field :id %> 
      <td valign="top"> 
       <%= pl.select :account_id, options_from_collection_for_select(@to_accounts, :id, :name, :selected => pl.object.account_id), :include_blank => true, :class=>"full" %> 
      </td> 

      <td><%= pl.text_field :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 => pl.object.id }, :remote => true unless index == 0 %></td> 
     </tr> 
    <% end %> 
    <%= f.submit %> 
<% end %> 

向您的PurchaseLineItem模型添加驗證以防止在沒有:account_id的情況下保存記錄。

class PurchaseLineItem < ActiveRecord::Base 
    belongs_to :purchase 
    validates_presence_of :account_id 
end 

其實如果你要驗證:account_id你不必使用:reject_if。

class Purchase < ActiveRecord::Base 
    has_many :purchase_line_items 
    accepts_nested_attributes_for :purchase_line_items, :allow_destroy =>true 

    validates_presence_of :purchase_line_items 
    validates_associated :purchase_line_items 
end 

爲了更加清楚,請編輯您的問題並添加create動作代碼。

相關問題