2016-11-21 134 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 /引用上。