我的 link_to_add_fields 代码适用于许多其他模型,但在这里它不起作用。我使用 Ryan Bates 的屏幕投射来模拟我的工作方式。
与此可能不同的是,我在一个部分中有 2 个嵌套模型。一半的表字段来自模型 a (journals),另一个来自模型 b (journal_entries),并且都基于 Leases 模型。
该表如下所示,[xx] 是输入字段。它看起来有点像数据网格。
Dated |Memo |Account |Credit |
-----------------------------------------------------------
[a.dated] |[a.memo] |[b.account_name] |[b.credit] |....
[a.dated] |[a.memo] |[b.account_name] |[b.credit] |....
[a.dated] |[a.memo] |[b.account_name] |[b.credit] |....
(Link:Click to add another row)
当我检查生成的 html 中的 link_to_add_fields 时,看到:
<a href="#" class="add_fields" data-fields="<fieldset style="
border:0px none;padding:0px .625em;"> </fieldset>"
data-id="-629772378">+ Add transactions</a>
我预计会有更多的行动......显然,当我点击链接时,什么也没有发生。
_journals_form.html.erb
<div>
<%= f.fields_for :journals do |journals| %>
<%= render "journal_fields" , {f: journals} %>
<% end %>
<%= link_to_add_fields "+ Add transactions", f, :journals %>
</div>
_journal_fields.html.erb,注意嵌套的 f.fields_for 以及 f.attribute 和 g.attribute 的混合
<fieldset style="border:0px none;padding:0px .625em;">
<%= f.fields_for :journal_entries do |g| %>
<table>
<tr>
<td style="width:200px;font-size:.7em"><%= f.date_select :dated %></td>
...
<td style="width:100px;"><%= g.text_field :account_name, size: 8 %></td>
<td style="width:100px;"><%= g.hidden_field :_destroy %><%= link_to "remove", '#', class: "remove_fields" %></td>
</tr>
</table>
<% end %>
</fieldset>
我的控制器似乎编辑/更新并添加了两个模型(期刊和 journal_entries)的行,除了删除链接有问题......
def edit
@lease = Lease.find(params[:id])
@lease.tenants.build
1.times do
journal = @lease.journals.build
journal.journal_entries.build
end
end
def update
@lease = Lease.find(params[:id])
if @lease.update_attributes(params[:lease])
redirect_to edit_pm_lease_path(@lease), notice: 'Lease was successfully updated.'
else
render action: "edit"
end
end
该模型基于租赁。租赁有许多期刊和许多通过期刊的期刊条目。这就是我们跟踪给定租赁的付款和账单的方式。Leases 也有很多租户(这个 add_fields... 效果很好)。
租赁.rb
class Lease < ActiveRecord::Base
...
has_many :tenants
has_many :journals, :order => [:dated, :id] #, :conditions => "journals.lease_id = id"
has_many :journal_entries, :through => :journals
accepts_nested_attributes_for :tenants , :reject_if => lambda { |a| a[:name].blank? }, :allow_destroy => true
accepts_nested_attributes_for :journal_entries , :allow_destroy => true
accepts_nested_attributes_for :journals , :allow_destroy => true
#Below didn't seem to help or hinder so it's removed.
#accepts_nested_attributes_for :journals, :journal_entries, :allow_destroy => true
...
end
期刊.rb
class Journal < ActiveRecord::Base
...
belongs_to :lease, :conditions => :lease_id != nil
has_many :journal_entries
accepts_nested_attributes_for :journal_entries , :allow_destroy => true
end
日记条目.rb
class JournalEntry < ActiveRecord::Base
belongs_to :journal
end
link_to_add_fields 助手:(来自 Ryan bates railscasts 的参考资料(EPISODE #196)
module ApplicationHelper
def external_link_to(label, target, options = [])
#length = 25 #options[:length] ||= 25
#window = options[:target] ||= "new"
unless target.downcase.start_with?("http://","https://")
link = "http://" + target.strip
end
link_to target, link, :target => "new"
end
def link_to_add_fields(name, f, association)
new_object = f.object.send(association).klass.new
id = new_object.object_id
fields = f.fields_for(association, new_object, child_index: id) do |builder|
render(association.to_s.singularize + "_fields", f: builder)
end
link_to(name, '#', class: "add_fields", data: {id: id, fields: fields.gsub("\n", "")})
end
end
不知道我在哪里出错了,数据的添加和删除工作正常,但是只有 journal/journal_entry 表单的 link_to_add_fields 没有做出应有的响应。我没有使用nested_form gem,如果这有所作为......
我正在使用 Rails 3.2.12、Ruby 1.9.3
任何帮助将不胜感激。