嵌套的link_to_function / insert_html会导致javascript错误

时间:2009-10-19 02:36:47

标签: ruby-on-rails partial-views rjs

我使用link_to_function / insert_html将部分插入页面。一切正常,直到我将另一个link_to_function / insert_html添加到正在呈现到页面中的部分。外部link_to_function链接突然停止工作并抛出此错误:

  

在属性列表后“丢失”“[中断   关于这个错误]   t.insert(“content_items_1”,{bottom:   “... [] [position] \”type = \“hidden \”   值=“

我认为html / js块被双重渲染到引用链接的事实导致了问题。这似乎是rails的错误,但这似乎也很常见。我错过了什么吗?任何简单的解决方法?

代码:主页

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

代码:部分'章'

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

这是不起作用的链接(添加章节链接) - 将\ 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>

1 个答案:

答案 0 :(得分:0)

错误似乎表明您的样式表缺少结束}。