我看到this railscast中的一些注解,该解决方案对Rails 3和jQuery不起作用,但目前尚不清楚。这是我在控制台:得到错误Railscasts与Rails 3不兼容?需要帮助调试
Uncaught ReferenceError: add_fields is not defined
(anonymous function)485:98
onclickd
它指出了这个链接是很长的和奇怪:<a href="#" onclick="add_fields(this, 'comment_titles', '&lt;p class=\&quot;fields\&quot;&gt;\n&lt;label for=\&quot;video_comment_titles_attributes_new_comment_titles_title\&quot;&gt;Comment Title:&lt;\/label&gt; \n&lt;input id=\&quot;video_comment_titles_attributes_new_comment_titles_title\&quot; name=\&quot;video[comment_titles_attributes][new_comment_titles][title]\&quot; size=\&quot;30\&quot; type=\&quot;text\&quot; /&gt;\n&lt;input id=\&quot;video_comment_titles_attributes_new_comment_titles__destroy\&quot; name=\&quot;video[comment_titles_attributes][new_comment_titles][_destroy]\&quot; type=\&quot;hidden\&quot; value=\&quot;false\&quot; /&gt;&lt;a href=\&quot;#\&quot; onclick=\&quot;remove_fields(this); return false;\&quot;&gt;remove&lt;\/a&gt;\n&lt;input id=\&quot;add_comment_title\&quot; name=\&quot;commit\&quot; type=\&quot;submit\&quot; value=\&quot;Add\&quot; /&gt;\n &lt;div class=\'hint\'&gt;Let your listeners know what comments you want by adding a guiding title for them. Pose a question, ask for feedback, or anything else!&lt;\/div&gt;\n&lt;\/p&gt;'); return false;">New Comment Title</a>
它这是该链接在我的视频节目的看法:
<%= link_to_add_fields "New Comment Title", f, :comment_titles %>
这是在我的应用助手link_to_add_fields方法:
def link_to_add_fields(name, f, association)
new_object = f.object.class.reflect_on_association(association).klass.new
fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
render(association.to_s.singularize + "_fields", :f => builder)
end
link_to_function(name, h("add_fields(this, '#{association}', '#{escape_javascript(fields)}')"))
end
这是场功能在我的application.js附加:从railscast
function add_fields(link, association, content) {
var new_id = new Date().getTime();
var regexp = new RegExp("new_" + association, "g");
$(link).parent().before(content.replace(regexp, new_id));
}
我几乎复制并粘贴所以我不知道发生了什么事。
我该如何解决这个问题?你还想看什么其他的代码?
你的问题似乎是在你的application.js所以修复它,你可以在以后调用你的add_fields方法。 – shingara 2011-04-04 06:37:30