2011-04-02 100 views
0

我有這種形式,我想通過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渲染工作?

回答

0

這基本上告訴你@video沒有定義。如果是這種情況,您可以在新方法中定義它,或者使用:video創建一個新的實例變量,而不是在頁面上下文中查找一個。然後,您可以處理更新方法中的查找和更新。

編輯:jQuery的負載方法

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

請你展開或提供的例子嗎? – 2011-04-02 08:18:30

+0

另外,我不確定爲什麼會發生這種情況......在視頻節目視圖中定義了「@ video」,除非它沒有在'topicables/new.html.erb'中定義的事實? – 2011-04-02 08:21:26

+0

也可能是'render:file =>'不會在該動作的上下文中加載文件。爲什麼你不想使用'render:action =>'new''? – picardo 2011-04-02 08:37:24