link_to_add_fieldsコードは他の多くのモデルで機能していますが、ここでは機能していません。私はライアンベイツのスクリーンキャストを使用して、私のやり方をモデル化しています。
これと異なるかもしれないのは、1つのパーシャルに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>
私のコントローラーは、削除リンクにバグがあることを除いて、両方のモデル(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
モデルはリースに基づいています。リースには、多くのジャーナルと、ジャーナルを介した多くのジャーナルエントリがあります。これは、特定のリースの支払いと請求を追跡する方法です。リースにも多くのテナントがあります(そしてこの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ヘルパー:(ライアンベイツレールキャストからの参照(エピソード#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_formgemを使用していません...
Rails 3.2.12、Ruby1.9.3を使用しています
どんな助けでも大歓迎です。