我正在创建一个状态板,它将显示多个服务器的特定详细信息,并且需要帮助来更新视图。
背景: 我创建了主要设置,其中包含以下内容:
- 保存称为服务器的服务器信息的 MySQL 数据库
- 使用引导程序显示内容
- 用于获取每个服务器信息并更新数据库的 ruby 脚本(在 application.rb 文件中称为 update_all_servers 的方法)
- 每分钟运行一次 ruby 脚本的 Cronjob
我需要帮助: 基本上,我需要有关我的 rails 应用程序的 javascript 部分的帮助。我不太确定如何更新我拥有的表中每个服务器的各个属性。我正在寻找的是 javascript/ajax 代码将每隔 30 秒定期从数据库中获取更新的值,并在不刷新页面的情况下更新视图。
在我的 index.html 中,您可以看到我为 server.rhel_version 属性放置了一个 id="comments"。我在想我可以使用 $(#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 .
视图截图: