2016-11-21 89 views
2

在railscast ajax輪詢教程(#229)後面實現ajax輪詢。警告框在運行服務器後不會彈出。Ajax輪詢:不顯示警報框[rails]

應用程序/視圖/報價/ index.js.erb的:

alert('Hey'); 
QuotePoller.poll(); 

應用程序/資產/ javascrips/quotes.coffee:

@QuotePoller = 
    poll: -> 
    setTimeout @request, 1000 

    request: -> 
    $.get($('#quotes').data('url')) 

jQuery -> 
if $('#quotes').length > 0 
    QuotePoller.poll() 

應用程序/視圖/報價/ index.html的。 ERB:

<div id="quotes" data-url="<%= quotes_url %>"> 

<table> 
    <thead> 
    <tr> 
    <th>Content</th> 
    <th colspan="3"></th> 
    </tr> 
    </thead> 


    <tbody> 
    <% @quotes.each do |quote| %> 
    <tr> 
    <td><%= quote.content %></td> 
    <td><%= link_to 'Show', quote %></td> 
    <td><%= link_to 'Edit', edit_quote_path(quote) %></td> 
    <td><%= link_to 'Destroy', quote, method: :delete, data: { confirm: 'Are you sure?' } %></td> 
    </tr> 
    <% end %> 
    </tbody> 
</table> 
</div> 
+0

你能在你的瀏覽器是否index.js.erb'的'內容爲包括在內呢?然後我們會知道它是否是一個rails配置問題(「爲什麼這個內容沒有被髮送到客戶端?」)或JavaScript問題(「爲什麼瀏覽器不執行這個JavaScript?」) – alexanderbird

+0

@alexanderbird這是一個JavaScript問題,解決了它!謝謝! – nais

+1

太棒了!很高興它解決了。順便說一句,在StackOverflow解決自己的問題的約定是發佈解決方案作爲答案,然後接受它,在大多數情況下 - 請參閱http://stackoverflow.com/help/self-answer – alexanderbird

回答

0

加入這我的索引行動

應用程序/控制器/ quotes_controller.rb解決它:

respond_to do |f| 
    f.js { render :content_type => 'text/javascript' } 
    f.html 
end 
0

您需要在應用程序/資產/ Java腳本/ application.js中添加以下代碼:

$(function() { 
    $.getScript("/quotes.js"); 
}); 

彈出窗口將出現在localhost:3000 /引用上。