2012-07-19 101 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