Answer the question
In order to leave comments, you need to log in
How to make data feed c javascript for ruby on rails 4?
DD, the problem is the following, there is data displayed in a block as in the picture, how to write javascript for ruby so that this data on the page when a new record is added to the database, the new record changes the first one, and so on
, you need to update, a kind of live feed (as on twitter), now a new element has been added to the database, js has removed the old element on the page and inserted a new one, 8 elements per page
Please help.
Controller
class StaticPagesController < ApplicationController
def home
@feed_items = Post.order('id DESC').limit(8)
end
end
<div class="all_post_main">
<ul>
<% if @feed_items.any? %>
<%= render partial: 'posts/feed_item1', collection: @feed_items %>
<% end %>
</ul>
</div>
<code lang="javascript">
<script>
setInterval(function() {
lastId = $('.feed-item').first().data('id')
$.ajax({
type: "GET",
dataType: 'script',
url: "<%= static_pages_home_path %>",
data: { last_id: lastId }
})
}, 5000);
</script>
</code>
<li <li class="feed-item" data-id="<%= feed_item1.id %>">
<div class="row post_name">
<div class="col-md-2">
<%= avatar_for feed_item1.user, :size => "50x50" %>
</div>
<div class="col-md-6">
<%= link_to (truncate feed_item1.name, :length => 90), feed_item1 %>
</div>
<div class="col-md-4">
<div class="feed_price pull-right">
<% if feed_item1.price.to_f == 0 %>
цена
<% else %>
<%= number_to_currency feed_item1.price %>
<% end %>
</div>
</div>
</div>
<div class="row post_user">
<div class="col-md-12">
<%= truncate feed_item1.content, :length => 90 %>
</div>
</div>
</li>
<% if @feed_items.any? %>
<%= render partial: 'posts/feed_item1', collection: @feed_items %>
<% end %>
alert('aaa');
Answer the question
In order to leave comments, you need to log in
The question is not entirely clear. How to use js to insert data in the right place? Well, you change the DOM in accordance with your markup. And so that when the page is reloaded, sorting by new data is added in the request order by created_at.
def home
@feed_items = Post.order('created_at DESC').paginate(page: params[:page])
end
You have already written almost everything yourself.
setInterval(function() {
lastId = $('.feed-item').first().data('id')
$.ajax({
type: "GET",
dataType: 'script',
url: "<%= static_pages_home_path %>",
data: { last_id: lastId }
})
}, 5000);
setInterval(function() {
lastId = $('.feed-item').first().data('id')
$.ajax({
type: "GET",
dataType: 'JSON', //<---------
url: "<%= static_pages_home_path %>",
data: { last_id: lastId },
success: function(response) //<---------------
{
if (response != null)
{
$(response).each(function(i,o){
$(".container").append("<div>" + o.Name + "<br/> + o.Comment + "</div>");
});
}
}), 5000);
It's better to already use Server-sent events ActionController::Live
for this than to touch the server every 5 seconds. When the record is added, through ActionController::Live
you can pull the client and update the data. Google it this way.
railscasts.com/episodes/401-actioncontroller-live
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question