Rails深层嵌套/非嵌套形式

时间:2011-12-28 09:35:40

标签: ruby-on-rails forms nested-forms

我正在尝试使用2个嵌套对创建一个处理4个模型的表单...出错:你没有想到它就有了一个nil对象!您可能期望一个Array实例。评估nil时发生错误。

有人可以指导我这个吗?对于您的信息,我正在使用rails 3.0。我需要在相同的视图中处理4个模型...非常感谢你的帮助,因为这个让我疯了几天:)

这是控制器的代码:

def new
@pinvoice = Pinvoice.new
@journal = Journal.new
@compte = Compte.find(:all)
if Journal.last.nil?
then
@internal = 'Pinv1'
else
@internal = 'Pinv' + (Journal.last.id + 1).to_s
end
1.times {@pinvoice.pinvlines.build}
4.times {@journal.lignes.build}
respond_to do |format|
format.html # new.html.erb
format.xml  { render :xml => @pinvoice }
end
end

模型代码:

class Pinvoice < ActiveRecord::Base
has_many :pinvlines, :dependent => :destroy
accepts_nested_attributes_for :pinvlines
end

class Pinvline < ActiveRecord::Base
belongs_to :pinvoice
belongs_to :compte
belongs_to :pinvoice
end

class Ligne < ActiveRecord::Base
belongs_to :journal
belongs_to :compte
attr_accessor :should_destroy
def should_destroy?
should_destroy.to_i == 1
end
end

class Journal < ActiveRecord::Base
has_many :lignes, :dependent => :destroy
accepts_nested_attributes_for :lignes, :reject_if => lambda { |a| a[:montant].to_s == "0"}
attr_accessor :taux
attr_accessor :compte_tva
validates_presence_of :date
validates_presence_of :texte
validates_presence_of :montant_HTVA
validates_presence_of :banque
validates_presence_of :compte
before_update :delete_lignes
def delete_lignes
@lignes.each do |l|
if l.new_record?
l.save
else
l.destroy
end
end
end
end

以及形式中最重要的一个:

<%= javascript_include_tag 'javascript_pinvlines_fields_new' %>
<%= form_for @pinvoice do |f| %>
<% if @pinvoice.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@pinvoice.errors.count, "error") %> prohibited this pinvoice from being saved:</h2>
<ul>
<% @pinvoice.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :internal %>
<%= f.text_field :internal, :value => @internal %><br />
<%= f.label :contact %>
<%= f.text_field :contact, :id => 'test' %>
<%= f.label :date_facture %>
<%= f.date_select :date_facture %>
<%= f.label :montant_total %>
<%= f.text_field :montant_total %>
</p>
<h2>Details</h2>
<%= f.fields_for :pinvlines do |builder| %>
<%= render "pinvline_fields", :f => builder %>
<%end%>
<p><%= link_to_add_fields "Ajouter une ligne", f, :pinvlines%></p>
<h2>Jounal</h2>
<%= fields_for @journal do |j| %>
<%= j.label :date %><br />
<%= j.text_field :date, :size => 10 %> <i> au format YYYY-MM-DD </i><br />
<%= j.label :texte %><br />
<%= j.text_field :texte%><br />
<%= j.label :banque %><br />
<%= j.text_field :banque %><br />
<%= j.label :montant_hors_tva %><br />
<%= j.text_field :montant_HTVA%><br />
<%= j.label :tva_id %><br />
<%= j.text_field :tva_id %><br />
<%= j.label :taux %><br />
<%= j.text_field :taux %><br />
<%= j.label :compte_tva %><br />
<%= j.text_field :compte_tva %><br />
<%= j.label :montant_TVA %><br />
<%= j.text_field :montant_TVA%><br />
<%= j.label :montant %><br />
<%= j.text_field :montant %><br />
<%= j.label :compte %><br />
<%= j.text_field :compte %><br />
<%= j.label :internal %><br />
<%= j.text_field :internal, :value => @internal%><br />
<%= j.label :source %><br />
<%= j.text_field :source%>
<br />
<h2>Lignes</h2>
<div class='container'>
<div class="duplicate">
<%= j.fields_for :lignes do |j| %>
<%= j.label :date %>
<%= j.text_field :date, :size=> 8 %>
<%= j.label :compte_id %>
<%= j.text_field :compte_id, :size=> 8  %>
<%= j.label :montant %>
<%= j.text_field :montant, :size=> 8  %>
<%= j.label :debit %>
<%= j.text_field :debit, :size=> 8  %>
<%= j.label :credit %>
<%= j.text_field :credit, :size=> 8  %> 
<br />  
<% end %>
</div>
</div>
<%end%>
<p><%= f.submit %></p>
<% end %>

1 个答案:

答案 0 :(得分:0)

最后,我找到了一种方法来处理这个问题,并在模型级别进行回调。