我正在创建一个状态板,它将显示多个服务器的特定详细信息,并需要有关更新视图的帮助。
背景 我创建了主要设置,我有以下内容:
我需要帮助: 基本上,我需要帮助我的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 .
观点截图:
答案 0 :(得分:0)
您可以使用jQuery.ajax执行所有这些操作,如果您搜索,则可以轻松找到相关的问题/答案,例如How to fire AJAX request Periodically?