weixin_33701617 2017-02-28 16:37 采纳率: 0%
浏览 54

Ruby on Rails实时更新

Okay, what i am looking for is a way to get an ajax script to run in the background of a page and look to see if there is a new record in the database and if there has grab that record and display that on my page.

i also do not want to use plugins i would rather do this myself as i prefer to learn how to do it and easier to make changes if i need it for something different in the future

im quite new to ajax and ruby on rails so a bit of explaining may be in order

This is my controller currently when i a user updates the content it changes without the page refreshing but what i want is it to also get new data

    class NotificationsController < ApplicationController

  # GET /notifications
  # GET /notifications.json
  def index
    @notifications = Notification.all
  end

def show
  end

def update 
  @notification = Notification.find(params[:id])
  @notification.update_attributes(:content => params[:content][:content])
  respond_to do |format|
    format.html { redirect_to notifications_path }
    format.json { render json: @notification}
  end
end
end

This is the js file

$(document).ready(function(){
    $('input[type=submit]').on('click', function(event){

        var form = $(this).parent('form');
        event.preventDefault();

        $.ajax({
            type:"POST",
            url: form.attr('action'),
            data: form.serialize(),
            success: function(data){
            console.log('this worked')
            $('#'+ data.id).html(data.content)

                        },
            error: function(data){
                console.log('failed')
            },
            dataType: 'JSON'
        });
        });
    });

and this is my html file

<% content_for :titleheader do %>
NOTIFICATIONS
<% end %>

<h1>Listing Notifications</h1>

    <% @notifications.each do |notification| %>
    <%= form_for notification do |f| %>

    <%= notification.id %>
    <div id="<%= notification.id %>"><%=notification.content %></div>
    <%= notification.read %>

<%= text_field "content", "content" %>
    <%= f.submit "change status" %>
<% end %>
<% end %>

As you can tell i have not made a create action yet because im not sure if i am going to have to do this through ajax or something

help is very appreciated

  • 写回答

1条回答 默认 最新

  • weixin_33695450 2017-07-13 13:37
    关注

    Yes you will need to do this through AJAX using a process called polling. Unfortunately if you don't want to use any extra gems or plugins then this is the only "simple" solution. Polling is when the script regularly checks the server for updates.

    setInterval(function(){ ajax_check_and_apply_updates_function(); }, 10000);
    

    Would run your ajax to check for updates every 10 seconds.

    If your willing to open up to gems there is Faye or Pusher that you can use to be asynchronous.

    If someone knows of a way to be asynchronous with just ruby on rails and AJAX I really hope they tell us because that would be dope.

    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器