2

按照 railscast ajax polling tutorial(#229) 实现 ajax 轮询。运行服务器后不会弹出警报框。

app/views/quotes/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()

app/views/quotes/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>
4

2 回答 2

0

您需要在 app/assets/javascripts/application.js 中添加以下代码:

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

然后弹出窗口将出现在 localhost:3000/quotes 上。

于 2016-11-21T22:13:10.827 回答
0

通过将此添加到我的索引操作来解决它

应用程序/控制器/quotes_controller.rb:

respond_to do |f|
  f.js { render :content_type => 'text/javascript' }
  f.html
end
于 2016-11-21T22:23:54.200 回答