通过Ajax加载模板 – 错误

我有这个表单,我想通过AJAX加载到video节目视图中:

 {:action => "update"}, :remote => true do |f| %> 
"topic_field" %>

我在video节目视图中有这个链接,点击后,我希望它呈现表单:

  :js), :remote => true, :id => 'edit_topics_link' %> 

我把表单放在topicables / new.html.erb中,我有topicables / new.js.erb文件,其中包含:

 $(" 'topicables/new.html.erb') %>").insertAfter('.topic_holder'); $('#edit_topics_link').hide(); 

现在当我点击链接时,我的日志中出现错误。 这就是我的日志显示的内容:

 Started GET "/topicables/new.js" for 127.0.0.1 at Sat Apr 02 03:58:52 -0700 2011 Processing by TopicablesController#new as JS Rendered topicables/new.html.erb (0.7ms) Rendered topicables/new.js.erb (2.6ms) Completed in 15ms ActionView::Template::Error (undefined method `model_name' for NilClass:Class): 1:  {:action => "update"}, :remote => true do |f| %> 2: 
3: "topic_field" %> 4:
app/views/topicables/new.html.erb:1:in `_app_views_topicables_new_html_erb___1876131309_2172772700_0' app/views/topicables/new.js.erb:1:in `_app_views_topicables_new_js_erb__1367015167_2172792200_0' Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.0.4/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.1ms) Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.0.4/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (92.5ms) Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.0.4/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (98.9ms)

这是怎么回事? 如何修复此错误以使表单的AJAX呈现有效?

这基本上告诉你@video没有定义。 如果是这种情况,您可以在新方法中定义它或使用:video创建新的实例变量,而不是在页面上下文中查找。 然后,您可以在update方法中处理查找和更新。

编辑:jQuery加载方法

 $("#myButton").click(function(){ $("#target").load("/topicables/new #new_topicable"); });