通过Ajax加载模板 - 错误 [英] Loading a template via Ajax - Error

查看:158
本文介绍了通过Ajax加载模板 - 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <%= form_for @我想要通过AJAX加载到视频显示视图中video,:url => {:action => update},:remote => true do | f | %> 
< div class =field>
<%= f.text_field:topic_names,:class => topic_field%>
< / div>
<%= f.submit添加主题%>
<%end%>

我在视频显示视图中有这个链接,当点击时,我希望它呈现窗体:

 <%= link_to添加主题,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();

现在,当我点击链接,我的日志中有错误。这是我的日志显示:

 开始GET/topicables/new.js为127.0.0.1在星期四04 02 :58:52 -0700 2011 
处理由TopicablesController#new as JS
渲染的topicables / new.html.erb(0.7ms)
渲染的topicables / new.js.erb(2.6ms)
完成15ms

ActionView :: Template :: Error(未定义的方法`model_name'为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:在`_app_views_topicables_new_html_erb___1876131309_2172772700_0'
app / views / topicables / new.js.erb:1:在`_app_views_topicables_new_js_erb__1367015167_2172792200_0'

渲染/Library/Ruby/Gems/1.8/gems/actionpack-3.0.4/lib/action_dispatch/middleware/templates/rescues/_trace.erb(1.1ms)
渲染/Library/Ruby/Gems/1.8 /gems/actionpack-3.0.4/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb(92.5ms)
渲染/Library/Ruby/Gems/1.8/gems/actionpack-3.0.4/lib/救援/布局(98.9ms)中的action_dispatch / middleware / templates / rescues / template_error.erb

上?如何解决这个错误,使表单的AJAX呈现工作?

解决方案

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



编辑:jQuery加载方法

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


I have this form that I want to load via AJAX into the video show view:

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

I have this link in the video show view that when clicked, I want it to render the form:

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

I put the form in topicables/new.html.erb and I have topicables/new.js.erb file which has:

$("<%= escape_javascript render(:file => 'topicables/new.html.erb') %>").insertAfter('.topic_holder');
$('#edit_topics_link').hide();

Now when I click the link, I get an error in my logs. This is what my logs show:

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)

What's going on? How can I fix this error to make the AJAX rendering of the form work?

解决方案

This is basically telling you that @video is not defined. If that's the case you can either define it in your new method or use :video which creates a new instance variable rather than looking for one in the page context. Then you can handle the find and update in the update method.

Edit: jQuery load method

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

这篇关于通过Ajax加载模板 - 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆