验证错误重置我的值为什么

时间:2012-05-23 09:03:05

标签: ruby-on-rails-3 validation

我有一个模态“A”,它有一个带有has_many关联的line_item模型“B”。这意味着B与A相关联。我在A模型中对B进行了验证

validates_presence_f :B
 validates_associated :B

现在以我的形式我使用“fields_for”来保存B的值,如果我提交一个空白表格而不是 验证失败并显示行项目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>

1 个答案:

答案 0 :(得分: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操作的代码。

相关问题