English 中文(简体)
校验错误重设我的值为什么
原标题:Validation error reset my values why

我有一个模式“ A ”, 它有一个与 has_ many 关联的直线项目模型“ B ” 。 这意味着 B 与 A 关联。 我对 A 模式 B 的验证

validates_presence_f :B
 validates_associated :B

now in my form i have used "fields_for" to save values of B, if i submit a blank form than validation fails and displays an error message for presence of line item B, but the fields of B s disabled, i have to reshow their fields. Can any one predict why this is happening.

here is my model : Model 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>
问题回答

如果我正确理解你 你对巢状有问题

您为什么拥有 read only 属性为 : 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 %> 

将验证添加到您的购买LineItem 模型中, 以防止保存记录时没有: 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

要更加清晰, 请编辑您的问题, 并添加 < code> create action > 的代码 。





相关问题
Bind Button.IsEnabled to custom validation with XAML?

I am sorry I didn t know how to title my question any better, you name it if you got a good 1. I have an entity Contact. this person has navigation properties: Address, Phones (A collection of Phone)....

WPF - MVVM - NHibernate Validation

Im facing a bit of an issue when trying to validate a decimal property on domain object which is bound to a textbox on the view through the viewmodel. I am using NHibernate to decorate my property on ...

Wpf Combobox Limit to List

We are using Wpf Combobox to allow the user to do the following things: 1) select items by typing in the first few characters 2) auto complete the entry by filtering the list 3) suggesting the first ...

Rails 101 | validates_currency?

I ve searched high and low, but I could not find a solution, to what I think seems like a very common task. In a form I want to have a text input that accepts currency strings (i.e. $1,000,000 or ...

CodeIgniter form verification and class

I m using the form validation library and have something like this in the view <p> <label for="NAME">Name <span class="required">*</span></label> <?...

热门标签