Html rails将\u链接到\u添加\u字段不添加具有\u many:through(内置嵌套表单)的字段

Html rails将\u链接到\u添加\u字段不添加具有\u many:through(内置嵌套表单)的字段,html,ruby-on-rails,ruby-on-rails-3,nested-forms,Html,Ruby On Rails,Ruby On Rails 3,Nested Forms,我有链接到添加字段的代码,适用于许多其他模型,但在这里它不起作用。我使用Ryan Bates的屏幕模型来模拟我的工作 与此不同的是,我在一个部分中有两个嵌套模型。一半的表字段来自模型a(日记账),另一半来自模型b(日记账分录),并且所有字段都基于租赁模型 该表如下所示,[xx]是输入字段。它看起来有点像一个数据网格 Dated |Memo |Account |Credit | ------------------------------------

我有链接到添加字段的代码,适用于许多其他模型,但在这里它不起作用。我使用Ryan Bates的屏幕模型来模拟我的工作

与此不同的是,我在一个部分中有两个嵌套模型。一半的表字段来自模型a(日记账),另一半来自模型b(日记账分录),并且所有字段都基于租赁模型

该表如下所示,[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_字段时,请参见:

<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>
该模型基于租赁。租赁有许多日记账和通过日记账的许多日记账分录。这就是我们跟踪给定租约的付款和账单的方式。租约也有很多租户(这个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
我不确定这张表哪里出了问题,数据的添加和删除工作正常,但是只有日记账/日记账条目表单的link\u to\u add\u字段没有正常响应。我没有使用嵌套的形式宝石,如果这有区别的话

我使用的是Rails 3.2.12和Ruby 1.9.3


任何帮助都将不胜感激。

修改您的
日志字段.hrml.erb
文件,删除
代码博客的
字段。为租约类添加嵌套的将军

 <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>

#您的表单字段

Hi Ganesh,错误在于当我点击链接添加分部时,没有添加分部的字段,\u,这样我就可以在一个页面上添加多个“日记”条目。我不认为链接到添加字段被破坏,因为它适用于其他嵌套的属性模型,甚至适用于与租约形式相同的嵌套属性模型租户。它适用于has\u many关系,但我不知道它对has\u many through关系有多大作用。是的,我猜问题就在那个里的某个地方:通过关系在同一个部分上,但必须有一个简单的修复方法。我知道我们可以通过接受嵌套属性深入到很多层次,而不必通过关系我为您的问题编写了答案,请参见您的答案中的itI回复。也许我试图在我的方法中设置太多的捷径。如果我这样做,我将如何告诉它使用依赖的“g.”模型(在本例中,f的子记录是日记账分录模型)。比如f.object.child.每个?。也许我可以将上面的文件作为子模型日志条目,然后让它引用父对象,比如

  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
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
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
class JournalEntry < ActiveRecord::Base
  belongs_to :journal
end
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
 <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>