gpt4 book ai didi

html - rails link_to_add_fields 不使用 has_many :through (with nested form inside) 添加字段

转载 作者:行者123 更新时间:2023-11-28 01:58:38 24 4
gpt4 key购买 nike

我有适用于许多其他模型的 link_to_add_fields 代码,但在这里不起作用。我使用 Ryan Bates 的屏幕截图来模拟我的工作方式。

可能与此不同的是我在一个部分中有 2 个嵌套模型。一半的表字段来自模型 a(期刊),另一半来自模型 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="&lt;fieldset style=&quot;
border:0px none;padding:0px .625em;&quot;&gt; &lt;/fieldset&gt;"
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>

我的 Controller 似乎可以编辑/更新和添加两个模型(journals 和 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

该模型基于租约。租赁有许多日记帐和许多通过日记帐分录的日记帐分录。这就是我们跟踪给定租约的付款和账单的方式。租约也有很 Multi-Tenancy (这个 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

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 助手:(来自 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

如有任何帮助,我们将不胜感激。

最佳答案

修改您的 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>

关于html - rails link_to_add_fields 不使用 has_many :through (with nested form inside) 添加字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15381586/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com