javascriptruby-on-railsajaxruby-on-rails-5ajax-polling

Ajax polling: Doesn't display alert box [rails]


Implementing ajax polling following railscast ajax polling tutorial(#229). Alert box doesn't pop after running the sever.

app/views/quotes/index.js.erb:

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

app/assets/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>

Solution

  • Solved it by adding this to my index action

    app/controllers/quotes_controller.rb:

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