I know this is a popular topic, but I searched quite a bit, trying to find the answer and strangely did not find anything that could help ...
I have a basic Rails 3 application that displays database records in a table. A separate application sends new records to the database, and I would like the Rails application to poll and update to display the new content. Right now I have a terrible part of javascript that just reloads the whole page and proves that this is my road block. I have a search bar and control panels to delete a few items, but they both reset when the page was refreshed (again, I know this is horribly and terribly broken).
From what I read, I understand that I have to use AJAX to query the database and simply render the database records that are newer than the newest record displayed. How to accurately complete this is another story ...
Most of the tutorials that I found are out of date, and I was unable to run the legacy code. I'm new to AJAX and Rails, and I would like someone to have any recommendations, tips, tutorials, or personal criticism (: P) that might point me in the right direction.
Thanks!
EDIT: I have new posts that are being pulled onto the page, but they are not showing correctly. For some reason, they are not added as records to my table. Here is my code, I hope you guys could choose something that I missed: * Note β the queries are actually the names of my records in the database. Queries are a model.
opinions / queries / index.html.erb
<table class="center"> <div id="requests"> <tr> <th><%= sortable "Patient_Name", "Patient Name" %></th> <th><%= sortable "Room_Number", "Room Number" %></th> <th><%= sortable "Request" %></th> <th><%= sortable "Urgency" %></th> <th><%= sortable "count", "Request Count" %></th> <th><%= sortable "created_at", "Created At" %></th> <th></th> </tr> <%= render @requests %> </div> </table>
Request Partial: views / requests / _request.html.erb
<div class="request" data-time="<%= request.created_at.to_i %>"> <tr> <td><%= request.Patient_Name %></td> <td><%= request.Room_Number %></td> <td><%= request.Request %></td> <td><%= request.Urgency %></td> <td><%= request.count %></td> <td><%= request.created_at.getlocal.strftime("%r on %D") %></td> <td><%= link_to 'Remove', request, :confirm => 'Are you sure?', :method => :delete %></td> </tr> </div>
opinions / queries / index.js.erb
$('#requests').append("<%=raw escape_javascript(render(@requests))%>");
ajax ruby-on-rails ruby-on-rails-3
eriknelson
source share