2

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>
Jongware
  • 22,200
  • 8
  • 54
  • 100
niaS
  • 323
  • 4
  • 18
  • Can you check in your browser if the content of `index.js.erb` is being included? Then we'll know if it's a rails configuration question ("why is this content not being sent to the client?") or a JavaScript problem ("why is the browser not executing this JavaScript?") – alexanderbird Nov 21 '16 at 21:44
  • @alexanderbird it was a JavaScript problem, solved it! Thanks! – niaS Nov 21 '16 at 22:12
  • 1
    great! glad it's solved. By the way, at StackOverflow the convention for solving your own problem is to post the solution as an answer and then accept it, in most cases - see http://stackoverflow.com/help/self-answer – alexanderbird Nov 21 '16 at 22:17

2 Answers2

0

You need to add the following code in app/assets/javascripts/application.js:

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

The pop up will then appear on localhost:3000/quotes.

VishalTheBeast
  • 459
  • 3
  • 9
0

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
niaS
  • 323
  • 4
  • 18