2017-02-28 13 views
2

私が探しているのは、ページのバックグラウンドで実行されるajaxスクリプトを取得し、データベースに新しいレコードがあるかどうかを確認し、私のページにそれを記録して表示する。Ruby on rails live update

私はまた、私はそれを行う方法を学ぶことを好むとiがかなり新しい未来

イムで別の何かのためにそれを必要とする場合、変更を行うことが容易と私はむしろこれを自分で行うだろうプラグインを使用する必要はありませんレール上でAJAXとルビーはそう説明するのビットは、IAユーザーが爽やかな、それはページなしで変更内容を更新しますが、私は何をしたいが、それはまた、新しいデータ

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 
を取得するときにこれは私のコントローラが現在ある

順であってもよく、

これはjsファイルです電子

$(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' 
     }); 
     }); 
    }); 

とイムはわからない私はアヤックスか何かを介してこれを行うには持っているつもりです場合ので、あなたが、私はまだアクションを作成行っていない伝えることができるように、これは私のhtmlファイル

<% 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 %> 

です

ヘルプが非常にはいあなたはポーリングと呼ばれるプロセスを使用してAJAXを介してこれを実行する必要があります

+0

これは悪い質問ですか、誰も答えを知らないですか? – Ray

答えて

0

を高く評価しています。残念なことに、余分な宝石やプラグインを使用したくない場合、これは唯一の「シンプルな」解決策です。ポーリングは、スクリプトが定期的にサーバーの更新をチェックする場合です。

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

ajaxを実行して10秒ごとに更新を確認しますか。

宝石を開こうとするなら、非同期で使用できるFayeまたはPusherがあります。

レールとAJAXでルビーだけと非同期になる方法を知っている人は、それがドープになるので、私たちに本当に教えてくれることを願っています。