通过Ajax加载模板 - 错误

时间:2011-04-02 08:10:28

标签: ruby-on-rails ruby ajax debugging ruby-on-rails-3

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

<%= form_for @video, :url => {:action => "update"}, :remote => true do |f| %>
  <div class="field">
  <%= f.text_field :topic_names, :class => "topic_field" %>
  </div>
  <%= f.submit "Add Topic" %>
<% end %>

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

<%= link_to "Add Topics", new_topicable_path(@topicable, :format => :js), :remote => true, :id => 'edit_topics_link' %>

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

$("<%= escape_javascript render(:file => '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: <%= form_for @video, :url => {:action => "update"}, :remote => true do |f| %>
    2:   <div class="field">
    3:   <%= f.text_field :topic_names, :class => "topic_field" %>
    4:   </div>
  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呈现起作用?

1 个答案:

答案 0 :(得分:0)

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

编辑:jQuery加载方法

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