2012-07-19 134 views
0

我正在創建一個狀態板,它將顯示多個服務器的特定細節,並需要幫助更新視圖。Rails - 定期更新某些標籤而不刷新頁面

背景: 我已在主要設置創建,我有以下幾點:

  • ,保持的服務器信息保持稱爲服務器
  • 使用引導顯示內容MySQL數據庫
  • 抓取每個服務器信息並更新數據庫的ruby腳本(在application.rb文件中稱爲update_all_servers的方法)
  • 運行ruby腳本的Cronjob牛逼每分鐘

我需要什麼幫助: 基本上,我需要用我的Rails應用程序的JavaScript部分中提供幫助。我不太確定如何更新表中每個服務器的個別屬性。我在尋找的是javascript/ajax代碼將定期從數據庫中獲取更新值,每30秒更新一次視圖而不刷新頁面。

在我的index.html中,可以看到我爲server.rhel_version屬性放置了一個id =「comments」。我想我可以使用$(#註釋)。更新它。無論是在application.js文件或其他有效的/邏輯的方法。

以下是我所有的源代碼。如果你們可以引導我採取我應該採取的方法,並可能提供一些示例代碼,我會非常感激!

/views/servers/index.html.erb

<%- model_class = Server.new.class -%> 
<div class="page-header"> 
    <h1><%=t '.title', :default => model_class.model_name.human.pluralize %></h1> 
</div> 
<table class="table table-striped"> 
    <thead> 
    <tr> 
<!--  <th><%= model_class.human_attribute_name(:id) %></th> --> 
     <th><%= model_class.human_attribute_name(:hostname) %></th> 
     <th><%= model_class.human_attribute_name(:port) %></th> 
    <!-- <th><%= model_class.human_attribute_name(:username) %></th> 
     <th><%= model_class.human_attribute_name(:password) %></th> 
     <th><%= model_class.human_attribute_name(:ssh_username) %></th> 
     <th><%= model_class.human_attribute_name(:ssh_password) %></th> 
     <th><%= model_class.human_attribute_name(:source_branch) %></th> --> 
     <th><%= model_class.human_attribute_name(:source_revision) %></th> 
     <th><%= model_class.human_attribute_name(:release) %></th> 
     <th><%= model_class.human_attribute_name(:rhel_version) %></th> 
     <th><%= model_class.human_attribute_name(:gpu_type) %></th> 
     <th><%= model_class.human_attribute_name(:total_users) %></th> 
     <th><%= model_class.human_attribute_name(:current_users) %></th> 
     <th><%= model_class.human_attribute_name(:created_at) %></th> 
     <th><%=t '.actions', :default => t("helpers.actions") %></th> 
    </tr> 
    </thead> 
    <tbody> 
    <% @servers.each do |server| %> 
     <tr> 
    <!--  <td><%= link_to server.id, server_path(server) %></td> --> 
     <td><%= server.hostname %></td> 
     <td><%= server.port %></td> 
    <!--  <td><%= server.username %></td> 
     <td><%= server.password %></td> 
     <td><%= server.ssh_username %></td> 
     <td><%= server.ssh_password %></td> 
     <td><%= server.source_branch %></td> --> 
     <td><%= server.source_revision %></td> 
     <td><%= server.release %></td> 
     <td id="comments"><%= server.rhel_version %></td> 
     <td><%= server.gpu_type %></td> 
     <td><%= server.total_users %></td> 
     <td><%= server.current_users %></td> 
     <td><%=l server.created_at %></td> 
     <td> 
      <%= link_to t('.edit', :default => t("helpers.links.edit")), 
         edit_server_path(server), :class => 'btn btn-mini' %> 
      <%= link_to t('.destroy', :default => t("helpers.links.destroy")), 
         server_path(server), 
         :method => :delete, 
         :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')), 
         :class => 'btn btn-mini btn-danger' %> 
     </td> 
     </tr> 
    <% end %> 
    </tbody> 
</table> 

<%= link_to t('.new', :default => t("helpers.links.new")), 
      new_server_path, 
      :class => 'btn btn-primary' %> 

/controllers/servers_controller.rb

class ServersController < ApplicationController 
    # GET /servers 
    # GET /servers.json 
    def index 
    @servers = Server.all 

    update_all_servers 

    respond_to do |format| 
     format.html # index.html.erb 
     format.json { render json: @servers } 
    end 
    end 

    # GET /servers/1 
    # GET /servers/1.json 
    def show 
    @server = Server.find(params[:id]) 

    respond_to do |format| 
     format.html # show.html.erb 
     format.json { render json: @server } 
    end 
    end 

    # GET /servers/new 
    # GET /servers/new.json 
    def new 
    @server = Server.new 

    respond_to do |format| 
     format.html # new.html.erb 
     format.json { render json: @server } 
    end 
    end 

    # GET /servers/1/edit 
    def edit 
    @server = Server.find(params[:id]) 
    end 

    # POST /servers 
    # POST /servers.json 
    def create 
    @server = Server.new(params[:server]) 

    respond_to do |format| 
     if @server.save 
     format.html { redirect_to @server, notice: 'Server was successfully created.' } 
     format.json { render json: @server, status: :created, location: @server } 
     else 
     format.html { render action: "new" } 
     format.json { render json: @server.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # PUT /servers/1 
    # PUT /servers/1.json 
    def update 
    @server = Server.find(params[:id]) 

    respond_to do |format| 
     if @server.update_attributes(params[:server]) 
     format.html { redirect_to @server, notice: 'Server was successfully updated.' } 
     format.json { head :no_content } 
     else 
     format.html { render action: "edit" } 
     format.json { render json: @server.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /servers/1 
    # DELETE /servers/1.json 
    def destroy 
    @server = Server.find(params[:id]) 
    @server.destroy 

    respond_to do |format| 
     format.html { redirect_to servers_url } 
     format.json { head :no_content } 
    end 
    end 
end 

/assets/javascripts/application.js

// This is a manifest file that'll be compiled into application.js, which will include all the files 
// listed below. 
// 
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts, 
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path. 
// 
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the 
// the compiled file. 
// 
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD 
// GO AFTER THE REQUIRES BELOW. 
// 
//= require jquery 
//= require jquery_ujs 
//= require twitter/bootstrap 
//= require_tree . 

截圖來看: statusboard view

回答

0

你可以做所有這些與jQuery.ajax,如果你搜索,你可以很容易地找到相關的問題/答案,例如How to fire AJAX request Periodically?

+0

我想我也應該增加,怎麼會去關於通過javascript從數據庫獲取服務器值,如server.hostname,server.username等。 – Rahul 2012-07-20 13:57:35