通过Ajax加载模板 - 错误
问题描述:
我有这种形式,我想通过AJAX加载到视频节目的看法:通过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渲染工作?
答
这基本上告诉你@video
没有定义。如果是这种情况,您可以在新方法中定义它,或者使用:video
创建一个新的实例变量,而不是在页面上下文中查找一个。然后,您可以处理更新方法中的查找和更新。
编辑:jQuery的负载方法
$("#myButton").click(function(){
$("#target").load("/topicables/new #new_topicable");
});
请你展开或提供的例子吗? – 2011-04-02 08:18:30
另外,我不确定为什么会发生这种情况......在视频节目视图中定义了“@ video”,除非它没有在'topicables/new.html.erb'中定义的事实? – 2011-04-02 08:21:26
也可能是'render:file =>'不会在该动作的上下文中加载文件。为什么你不想使用'render:action =>'new''? – picardo 2011-04-02 08:37:24