Nested function ___ link / insert_html causes JavaScript errors

Im using link_to_function / insert_html to insert a partial page. Everything works fine until I add the link_to_function / insert_html link to the partial link that appears on the page. Suddenly, the link_to_function link breaks and throws this error:

"missing" after the property list "[Break by this error] t.insert (" content_items_1 ", {below:" ... [] [position] \ "type = \" hidden \ "value ="

I think the problem with the html / js block being displayed twice in the quoted link is causing problems. This seems to be a mistake with the rails, but this also seems to be quite common. Did I miss something? Any light work around?

Code: Home

  <ol id="chapters">
     <%= render :partial => 'chapter', :collection => @course.chapters %>
  </ol>
  <%= link_to_function("Add Chapter") do |page|
    page.insert_html :bottom, :chapters, :partial => 'chapter', :object => Chapter.new
  end %> 

: Partial 'chapter'

  <ol id="content_items_<%= @content_item_iter.to_s %>">
  </ol>
  <%= link_to_function "Add Content Item" do |page2|
   page2.insert_html :bottom, "content_items_#{@content_item_iter.to_s}", :partial => 'content_item', :object => ContentItem.new
   end %>

, ( Add Chapter) - \n,\n,\\n :

      <a href="#" onclick="try {
    Element.insert(&quot;chapters&quot;, { bottom: &quot;  
    &lt;li&gt;
      &lt;input id=\&quot;course_chapter_attributes_1_position\&quot; name=\&quot;course[chapter_attributes][1][position]\&quot; type=\&quot;hidden\&quot; value=\&quot;0\&quot; /&gt;
      &lt;h2&gt;
        &lt;label for=\&quot;course_chapter_attributes_1_name\&quot;&gt;Chapter:&lt;/label&gt;
        &lt;input id=\&quot;course_chapter_attributes_1_name\&quot; name=\&quot;course[chapter_attributes][1][name]\&quot; size=\&quot;30\&quot; type=\&quot;text\&quot; /&gt;
      &lt;/h2&gt;
      &lt;ol id=\&quot;content_items_1\&quot;&gt;
      &lt;/ol&gt;
      &lt;a href=\&quot;#\&quot; onclick=\&quot;try {
Element.insert(&quot;content_items_1&quot;, { bottom: &quot;        
          &lt;li&gt;
          &lt;input id=\\&quot;course_chapter_attributes__content_item_attributes__position\\&quot; name=\\&quot;course[chapter_attributes][][content_item_attributes][][position]\\&quot; type=\\&quot;hidden\\&quot; value=\\&quot;1\\&quot; /&gt;
          &lt;h3&gt;
            &lt;label for=\\&quot;course_chapter_attributes__content_item_attributes__description\\&quot;&gt;Article:&lt;/label&gt;
            &lt;input id=\\&quot;course_chapter_attributes__content_item_attributes__description\\&quot; name=\\&quot;course[chapter_attributes][][content_item_attributes][][description]\\&quot; size=\\&quot;30\\&quot; type=\\&quot;text\\&quot; /&gt; &lt;br /&gt;
          &lt;/h3&gt;
          &lt;label for=\\&quot;course_chapter_attributes__content_item_attributes__url\\&quot;&gt;Link URL:&lt;/label&gt;
          &lt;input id=\\&quot;course_chapter_attributes__content_item_attributes__url\\&quot; name=\\&quot;course[chapter_attributes][][content_item_attributes][][url]\\&quot; size=\\&quot;30\\&quot; type=\\&quot;text\\&quot; /&gt; &lt;br /&gt;
          &lt;label for=\\&quot;course_chapter_attributes__content_item_attributes__youtube_id\\&quot;&gt;Youtube Video ID:&lt;/label&gt;
          &lt;input id=\\&quot;course_chapter_attributes__content_item_attributes__youtube_id\\&quot; name=\\&quot;course[chapter_attributes][][content_item_attributes][][youtube_id]\\&quot; size=\\&quot;30\\&quot; type=\\&quot;text\\&quot; /&gt; &lt;br /&gt;
          &lt;label for=\\&quot;course_chapter_attributes__content_item_attributes__start_time\\&quot;&gt;Start / Stop Time:&lt;/label&gt;
          &lt;input id=\\&quot;course_chapter_attributes__content_item_attributes__start_time\\&quot; name=\\&quot;course[chapter_attributes][][content_item_attributes][][start_time]\\&quot; size=\\&quot;30\\&quot; type=\\&quot;text\\&quot; /&gt; &lt;input id=\\&quot;course_chapter_attributes__content_item_attributes__end_time\\&quot; name=\\&quot;course[chapter_attributes][][content_item_attributes][][end_time]\\&quot; size=\\&quot;30\\&quot; type=\\&quot;text\\&quot; /&gt;
          &lt;/li&gt;


&quot; });
} catch (e) { alert('RJS error:  ' + e.toString()); alert('Element.insert(\\&quot;content_items_1\\&quot;, { bottom: \\&quot;        
          &lt;li&gt;
          &lt;input id=\\\\\\&quot;course_chapter_attributes__content_item_attributes__position\\\\\\&quot; name=\\\\\\&quot;course[chapter_attributes][][content_item_attributes][][position]\\\\\\&quot; type=\\\\\\&quot;hidden\\\\\\&quot; value=\\\\\\&quot;1\\\\\\&quot; /&gt;
          &lt;h3&gt;
            &lt;label for=\\\\\\&quot;course_chapter_attributes__content_item_attributes__description\\\\\\&quot;&gt;Article:&lt;/label&gt;
            &lt;input id=\\\\\\&quot;course_chapter_attributes__content_item_attributes__description\\\\\\&quot; name=\\\\\\&quot;course[chapter_attributes][][content_item_attributes][][description]\\\\\\&quot; size=\\\\\\&quot;30\\\\\\&quot; type=\\\\\\&quot;text\\\\\\&quot; /&gt; &lt;br /&gt;
          &lt;/h3&gt;
          &lt;label for=\\\\\\&quot;course_chapter_attributes__content_item_attributes__url\\\\\\&quot;&gt;Link URL:&lt;/label&gt;
          &lt;input id=\\\\\\&quot;course_chapter_attributes__content_item_attributes__url\\\\\\&quot; name=\\\\\\&quot;course[chapter_attributes][][content_item_attributes][][url]\\\\\\&quot; size=\\\\\\&quot;30\\\\\\&quot; type=\\\\\\&quot;text\\\\\\&quot; /&gt; &lt;br /&gt;
          &lt;label for=\\\\\\&quot;course_chapter_attributes__content_item_attributes__youtube_id\\\\\\&quot;&gt;Youtube Video ID:&lt;/label&gt;
          &lt;input id=\\\\\\&quot;course_chapter_attributes__content_item_attributes__youtube_id\\\\\\&quot; name=\\\\\\&quot;course[chapter_attributes][][content_item_attributes][][youtube_id]\\\\\\&quot; size=\\\\\\&quot;30\\\\\\&quot; type=\\\\\\&quot;text\\\\\\&quot; /&gt; &lt;br /&gt;
          &lt;label for=\\\\\\&quot;course_chapter_attributes__content_item_attributes__start_time\\\\\\&quot;&gt;Start / Stop Time:&lt;/label&gt;
          &lt;input id=\\\\\\&quot;course_chapter_attributes__content_item_attributes__start_time\\\\\\&quot; name=\\\\\\&quot;course[chapter_attributes][][content_item_attributes][][start_time]\\\\\\&quot; size=\\\\\\&quot;30\\\\\\&quot; type=\\\\\\&quot;text\\\\\\&quot; /&gt; &lt;input id=\\\\\\&quot;course_chapter_attributes__content_item_attributes__end_time\\\\\\&quot; name=\\\\\\&quot;course[chapter_attributes][][content_item_attributes][][end_time]\\\\\\&quot; size=\\\\\\&quot;30\\\\\\&quot; type=\\\\\\&quot;text\\\\\\&quot; /&gt;
          &lt;/li&gt;



\\&quot; });'); throw e }; return false;\&quot;&gt;Add Content Item&lt;/a&gt;
    &lt;/li&gt;


&quot; });
    } catch (e) { alert('RJS error:

' + e.toString()); alert('Element.insert(\&quot;chapters\&quot;, { bottom: \&quot;  
    &lt;li&gt;
      &lt;input id=\\\&quot;course_chapter_attributes_1_position\\\&quot; name=\\\&quot;course[chapter_attributes][1][position]\\\&quot; type=\\\&quot;hidden\\\&quot; value=\\\&quot;0\\\&quot; /&gt;
      &lt;h2&gt;
        &lt;label for=\\\&quot;course_chapter_attributes_1_name\\\&quot;&gt;Chapter:&lt;/label&gt;
        &lt;input id=\\\&quot;course_chapter_attributes_1_name\\\&quot; name=\\\&quot;course[chapter_attributes][1][name]\\\&quot; size=\\\&quot;30\\\&quot; type=\\\&quot;text\\\&quot; /&gt;
      &lt;/h2&gt;
      &lt;ol id=\\\&quot;content_items_1\\\&quot;&gt;
      &lt;/ol&gt;
      &lt;a href=\\\&quot;#\\\&quot; onclick=\\\&quot;try {
Element.insert(&quot;content_items_1&quot;, { bottom: &quot;        
          &lt;li&gt;
          &lt;input id=\\\\&quot;course_chapter_attributes__content_item_attributes__position\\\\&quot; name=\\\\&quot;course[chapter_attributes][][content_item_attributes][][position]\\\\&quot; type=\\\\&quot;hidden\\\\&quot; value=\\\\&quot;1\\\\&quot; /&gt;
          &lt;h3&gt;
            &lt;label for=\\\\&quot;course_chapter_attributes__content_item_attributes__description\\\\&quot;&gt;Article:&lt;/label&gt;
            &lt;input id=\\\\&quot;course_chapter_attributes__content_item_attributes__description\\\\&quot; name=\\\\&quot;course[chapter_attributes][][content_item_attributes][][description]\\\\&quot; size=\\\\&quot;30\\\\&quot; type=\\\\&quot;text\\\\&quot; /&gt; &lt;br /&gt;
          &lt;/h3&gt;
          &lt;label for=\\\\&quot;course_chapter_attributes__content_item_attributes__url\\\\&quot;&gt;Link URL:&lt;/label&gt;
          &lt;input id=\\\\&quot;course_chapter_attributes__content_item_attributes__url\\\\&quot; name=\\\\&quot;course[chapter_attributes][][content_item_attributes][][url]\\\\&quot; size=\\\\&quot;30\\\\&quot; type=\\\\&quot;text\\\\&quot; /&gt; &lt;br /&gt;
          &lt;label for=\\\\&quot;course_chapter_attributes__content_item_attributes__youtube_id\\\\&quot;&gt;Youtube Video ID:&lt;/label&gt;
          &lt;input id=\\\\&quot;course_chapter_attributes__content_item_attributes__youtube_id\\\\&quot; name=\\\\&quot;course[chapter_attributes][][content_item_attributes][][youtube_id]\\\\&quot; size=\\\\&quot;30\\\\&quot; type=\\\\&quot;text\\\\&quot; /&gt; &lt;br /&gt;
          &lt;label for=\\\\&quot;course_chapter_attributes__content_item_attributes__start_time\\\\&quot;&gt;Start / Stop Time:&lt;/label&gt;
          &lt;input id=\\\\&quot;course_chapter_attributes__content_item_attributes__start_time\\\\&quot; name=\\\\&quot;course[chapter_attributes][][content_item_attributes][][start_time]\\\\&quot; size=\\\\&quot;30\\\\&quot; type=\\\\&quot;text\\\\&quot; /&gt; &lt;input id=\\\\&quot;course_chapter_attributes__content_item_attributes__end_time\\\\&quot; name=\\\\&quot;course[chapter_attributes][][content_item_attributes][][end_time]\\\\&quot; size=\\\\&quot;30\\\\&quot; type=\\\\&quot;text\\\\&quot; /&gt;
          &lt;/li&gt;



&quot; });
} catch (e) { alert(\'RJS error:

\' + e.toString()); alert(\'Element.insert(\\\\&quot;content_items_1\\\\&quot;, { bottom: \\\\&quot;        
          &lt;li&gt;
          &lt;input id=\\\\\\\\\\\\&quot;course_chapter_attributes__content_item_attributes__position\\\\\\\\\\\\&quot; name=\\\\\\\\\\\\&quot;course[chapter_attributes][][content_item_attributes][][position]\\\\\\\\\\\\&quot; type=\\\\\\\\\\\\&quot;hidden\\\\\\\\\\\\&quot; value=\\\\\\\\\\\\&quot;1\\\\\\\\\\\\&quot; /&gt;
          &lt;h3&gt;
            &lt;label for=\\\\\\\\\\\\&quot;course_chapter_attributes__content_item_attributes__description\\\\\\\\\\\\&quot;&gt;Article:&lt;/label&gt;
            &lt;input id=\\\\\\\\\\\\&quot;course_chapter_attributes__content_item_attributes__description\\\\\\\\\\\\&quot; name=\\\\\\\\\\\\&quot;course[chapter_attributes][][content_item_attributes][][description]\\\\\\\\\\\\&quot; size=\\\\\\\\\\\\&quot;30\\\\\\\\\\\\&quot; type=\\\\\\\\\\\\&quot;text\\\\\\\\\\\\&quot; /&gt; &lt;br /&gt;
          &lt;/h3&gt;
          &lt;label for=\\\\\\\\\\\\&quot;course_chapter_attributes__content_item_attributes__url\\\\\\\\\\\\&quot;&gt;Link URL:&lt;/label&gt;
          &lt;input id=\\\\\\\\\\\\&quot;course_chapter_attributes__content_item_attributes__url\\\\\\\\\\\\&quot; name=\\\\\\\\\\\\&quot;course[chapter_attributes][][content_item_attributes][][url]\\\\\\\\\\\\&quot; size=\\\\\\\\\\\\&quot;30\\\\\\\\\\\\&quot; type=\\\\\\\\\\\\&quot;text\\\\\\\\\\\\&quot; /&gt; &lt;br /&gt;
          &lt;label for=\\\\\\\\\\\\&quot;course_chapter_attributes__content_item_attributes__youtube_id\\\\\\\\\\\\&quot;&gt;Youtube Video ID:&lt;/label&gt;
          &lt;input id=\\\\\\\\\\\\&quot;course_chapter_attributes__content_item_attributes__youtube_id\\\\\\\\\\\\&quot; name=\\\\\\\\\\\\&quot;course[chapter_attributes][][content_item_attributes][][youtube_id]\\\\\\\\\\\\&quot; size=\\\\\\\\\\\\&quot;30\\\\\\\\\\\\&quot; type=\\\\\\\\\\\\&quot;text\\\\\\\\\\\\&quot; /&gt; &lt;br /&gt;
          &lt;label for=\\\\\\\\\\\\&quot;course_chapter_attributes__content_item_attributes__start_time\\\\\\\\\\\\&quot;&gt;Start / Stop Time:&lt;/label&gt;
          &lt;input id=\\\\\\\\\\\\&quot;course_chapter_attributes__content_item_attributes__start_time\\\\\\\\\\\\&quot; name=\\\\\\\\\\\\&quot;course[chapter_attributes][][content_item_attributes][][start_time]\\\\\\\\\\\\&quot; size=\\\\\\\\\\\\&quot;30\\\\\\\\\\\\&quot; type=\\\\\\\\\\\\&quot;text\\\\\\\\\\\\&quot; /&gt; &lt;input id=\\\\\\\\\\\\&quot;course_chapter_attributes__content_item_attributes__end_time\\\\\\\\\\\\&quot; name=\\\\\\\\\\\\&quot;course[chapter_attributes][][content_item_attributes][][end_time]\\\\\\\\\\\\&quot; size=\\\\\\\\\\\\&quot;30\\\\\\\\\\\\&quot; type=\\\\\\\\\\\\&quot;text\\\\\\\\\\\\&quot; /&gt;
          &lt;/li&gt;



\\\\&quot; });\'); throw e }; return false;\\\&quot;&gt;Add Content Item&lt;/a&gt;
    &lt;/li&gt;


\&quot; });'); throw e }; return false;">Add Chapter</a>
+3
1

, , , } .

0

Source: https://habr.com/ru/post/1720416/


All Articles