![](/img/trans.png)
[英]Rails 3.2 Coffeescript to Refresh Div using Ajax - Reloading entire page
[英]Rails AJAX loads entire page into 'update' div
我想要一些可以自動重新加載帖子並將其插入div post_container的代碼:
<%= link_to_remote "Update", {:action => 'ajax_update', :id => @posts, :update => 'post_container'}, :confirm => "Are you sure?" %>
我的home.html.erb中的這個rails片段實際上占據了整個頁面(標題,標題,正文標簽),並將其放置在div post_container中。 為什么? 而且,據我所知,ajax_update函數甚至沒有被調用。
我該怎么做? 為什么會發生整個頁面加載? 我正在使用Rails 2.3.11
(編輯:同樣,單擊鏈接時沒有確認對話框。)
編輯2:代碼段的html輸出:
<a confirm="Are you sure?" href="#" onclick="new Ajax.Updater('post_container', '/home', {asynchronous:true, evalScripts:true, parameters:'authenticity_token=' + encodeURIComponent('uaqM0Ie8to5pprvE6WcF416DN0vNeyO7Xa+JM6VZFY4=')}); return false;">Update</a>
在您的控制器操作中,使用:layout => false
def ajax_update
@posts = Post.find(@posts)
render :layout => false
end
找到正確的方法:
<%= link_to_remote "Update",:update => 'post_container', :url => {:action => 'ajax_update', :id => @posts, }, :confirm => "Are you sure?" %>
基於Rails 2.3.11 API文檔中的信息:
http://api.rubyonrails.org/v2.3.11/classes/ActionView/Helpers/PrototypeHelper.html#M002185
此外,在該頁面上找到的periodic_call_remote方法對於不需要單擊的自動呼叫也很有用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.