我有link_to_add_fields代码为许多其他模型工作,但这里它不工作。我用瑞安·贝茨(Ryan Bates)的演员模型来模拟我的表演。
可能与此不同的是,我在一个部分中有两个嵌套模型。一半的表字段来自模型a(日志),另一半来自模型b (journal_entries),所有这些都基于租约模型。
表如下所示,[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)
当我检查link_to_add_fields在生成的html见:
<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。请注意嵌套的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
该模型基于租约。租赁有许多日记账,通过日记账有许多日记账分录。这是我们记录特定租赁的付款和账单的方式。租约也有许多租户(和add_fields…伟大的工作)。
Lease.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
journal.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
journal_entry.rb
class JournalEntry < ActiveRecord::Base
belongs_to :journal
end
link_to_add_fields helper:(引用自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
不确定我在哪里错了这个,数据的添加和删除正在工作,但link_to_add_fields仅用于日志/journal_entry表单没有响应,因为它应该。我不使用nested_form gem,如果这有区别…
我使用的是Rails 3.2.12, Ruby 1.9.3
修改您的journal_fields.hrml.erb
文件,删除fields_for
代码博客。为Lease类添加嵌套通用。
<fieldset style="border:0px none;padding:0px .625em;">
<table>
<tr>
<td style="width:200px;font-size:.7em"><%= f.date_select :dated %></td>
# Your form field
<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>
</fieldset>