【问题标题】:rails link_to_add_fields not adding fields with has_many :through (with nested form inside)rails link_to_add_fields 不添加带有 has_many 的字段:通过(内部有嵌套表单)
【发布时间】:2013-03-13 09:38:48
【问题描述】:

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

与此不同的是,我在一个部分中有 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="&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... 效果很好)。

租赁.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

不确定我在哪里出错了,数据的添加和删除工作正常,但仅用于日志/日志条目表单的 link_to_add_fields 没有做出应有的响应。我没有使用nested_form gem,如果这有影响的话......

我正在使用 Rails 3.2.12、Ruby 1.9.3

任何帮助将不胜感激。

【问题讨论】:

  • 嗨 Ganesh,错误是当我单击链接以添加部分时,它没有添加 fields_for 部分,以便我可以在一页上添加许多“期刊”条目。我不认为 link_to_add_fields 被破坏了,因为它适用于其他nested_attribute 模型,它甚至适用于nested_attribute 模型 Tenants 是在相同的租赁表格上。
  • 对于 has_many 关系来说效果很好,但我不知道 has_many through 关系的效果如何。
  • 是的,我猜问题出在某个地方, :through 关系在同一个部分,但必须有一个简单的解决方法。我知道我们可以通过 accept_nested_attributes_for 深入很多层次...
  • 没有通过关系我为你的问题写了答案,看看它
  • 我在您的回答中做出了回应。也许我试图在我的方法中走捷径。

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


【解决方案1】:

通过删除 fields_for 代码博客来修改您的 journal_fields.hrml.erb 文件。为 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>

【讨论】:

  • 如果我这样做,我将如何告诉它与依赖的'g'一起工作。模型(f 的子记录在这种情况下是 Journal_entries 模型)。像 f.object.child.each 之类的东西?也许如果我可以将上面的fileds_for设置为子模型journal_entries,然后让它引用父对象... &lt;%f.object.parent.date_select :dated %&gt; &lt;%f.text_field :account_name%&gt;
猜你喜欢
  • 2016-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-12
  • 2011-01-12
  • 1970-01-01
  • 2011-06-30
  • 1970-01-01
相关资源
最近更新 更多