3

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!

Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
syncn
  • 31
  • 1
  • The problem is the `link_to_add_assocation` will pre-render a partial, which contains a `link_to_add_association`, which will pre-render a partial, which contains ... You get the picture I hope :) So I am not sure cocoon is the correct solution for this. – nathanvda Dec 23 '14 at 01:37
  • @nathanvda thanks! yeah, that's the point and now I understand cocoon is not the right solution. If I still want to get this working, what would you recommend? Render form via js? – syncn Dec 25 '14 at 01:42

0 Answers0