Currently, I'm building a Medium-like blog service which users can add article parts(text, image, youtube...etc) that composite the article body.
Article model has many ArticlePart model and to create nested form dynamically, I'm using cocoon gem. Here's my current code.
_form.html.erb
<%= link_to_add_association("Add New Article Part", f, :article_parts,{:data => {"association-insertion-node" => "#article" }}) %>
<ul class="sortable">
<div id="article">
<%= f.fields_for :article_parts %>
</div>
</ul>
_article_part_fields.html.erb
<li class="nested-fields">
<h2>Text Part</h2>
<%= f.hidden_field :position %>
<%= f.label :title %>
<%= f.text_field :title %>
<%= link_to_remove_association("Delete part", f) %>
<h2>Insert New Article Part Here</h2>
</li>
Above works fine but what I want to do is to display "link_to_add_association" where "Insert New Article Part Here" blocks in _article_part_fields.html.erb so that users can add new article part anywhere they want to.
I tried to do it as below by passing parent object form to nested object form, but this code causes stack level too deep error.
<li class="nested-fields">
<h2>Text Parts</h2>
<%= f.hidden_field :position, :class => "position" %>
<%= f.label :title %>
<%= f.text_field :title, :class => "fld-name required" %>
<%= link_to_remove_association("Delete part", f) %>
<h2>Insert New Part Here</h2>
<%= link_to_add_association("商品", parent_form, :article_parts,{:render_options => {:locals => {:parent_form => parent_form}}, :data => {"association-insertion-node" => "#article" }}, :class => 'btn btn-ctrl btn-lg') %>
</li>
Any help appreciated to get this working, or suggest another way to achieve this.
Thanks in advance!