rails link_to_add_fields不添加带has_many的字段:through(内置嵌套表单)

时间:2013-03-13 09:38:48

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

我有link_to_add_fields代码适用于许多其他型号,但在这里它不起作用。我使用Ryan Bates的屏幕演员模拟我的演出方式。

与此不同的是,我在一个局部中有2个嵌套模型。表格字段的一半来自模型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)

当我检查生成的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>

我的控制器似乎编辑/更新并添加了两个模型(日志和日记条目)的行,只是删除链接有错误......

  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 ......效果很好)。

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帮助:(来自Ryan bates railcasts的参考文献(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

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

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