Note that there are some explanatory texts on larger screens.

plurals
  1. PORails - periodically update certain tags without refreshing the page
    text
    copied!<p>I am creating a statusboard that will show specific details of several servers and need help with updating the view.</p> <p><strong>Background:</strong> I have the main setup created where I have the following: </p> <ul> <li>A MySQL database that keeps hold of the server information called servers</li> <li>Using bootstrap to show the content</li> <li>A ruby script to grab each servers information and updating the database (method called update_all_servers in the application.rb file)</li> <li>Cronjob that runs the ruby script every minute</li> </ul> <p><strong>What I need help with:</strong> Basically, I need help with the javascript part of my rails app. I am not too sure exactly how too update individual attributes of each server in the table I have. What I am looking for is that the javascript/ajax code will periodically grab the updated values from the database every 30 seconds and update the view without refreshing the page. </p> <p>In my index.html, you can see that I have placed an id="comments" for the server.rhel_version attribute. I was thinking I could use the $(#comments). to update it. Either in the application.js file or some other efficient/logical method. </p> <p>Below is all my source code. If you guys could guide me on the approach I should take and possibly provide some sample code, I would really appreciate it!</p> <p><strong>/views/servers/index.html.erb</strong></p> <pre><code>&lt;%- model_class = Server.new.class -%&gt; &lt;div class="page-header"&gt; &lt;h1&gt;&lt;%=t '.title', :default =&gt; model_class.model_name.human.pluralize %&gt;&lt;/h1&gt; &lt;/div&gt; &lt;table class="table table-striped"&gt; &lt;thead&gt; &lt;tr&gt; &lt;!-- &lt;th&gt;&lt;%= model_class.human_attribute_name(:id) %&gt;&lt;/th&gt; --&gt; &lt;th&gt;&lt;%= model_class.human_attribute_name(:hostname) %&gt;&lt;/th&gt; &lt;th&gt;&lt;%= model_class.human_attribute_name(:port) %&gt;&lt;/th&gt; &lt;!-- &lt;th&gt;&lt;%= model_class.human_attribute_name(:username) %&gt;&lt;/th&gt; &lt;th&gt;&lt;%= model_class.human_attribute_name(:password) %&gt;&lt;/th&gt; &lt;th&gt;&lt;%= model_class.human_attribute_name(:ssh_username) %&gt;&lt;/th&gt; &lt;th&gt;&lt;%= model_class.human_attribute_name(:ssh_password) %&gt;&lt;/th&gt; &lt;th&gt;&lt;%= model_class.human_attribute_name(:source_branch) %&gt;&lt;/th&gt; --&gt; &lt;th&gt;&lt;%= model_class.human_attribute_name(:source_revision) %&gt;&lt;/th&gt; &lt;th&gt;&lt;%= model_class.human_attribute_name(:release) %&gt;&lt;/th&gt; &lt;th&gt;&lt;%= model_class.human_attribute_name(:rhel_version) %&gt;&lt;/th&gt; &lt;th&gt;&lt;%= model_class.human_attribute_name(:gpu_type) %&gt;&lt;/th&gt; &lt;th&gt;&lt;%= model_class.human_attribute_name(:total_users) %&gt;&lt;/th&gt; &lt;th&gt;&lt;%= model_class.human_attribute_name(:current_users) %&gt;&lt;/th&gt; &lt;th&gt;&lt;%= model_class.human_attribute_name(:created_at) %&gt;&lt;/th&gt; &lt;th&gt;&lt;%=t '.actions', :default =&gt; t("helpers.actions") %&gt;&lt;/th&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt; &lt;% @servers.each do |server| %&gt; &lt;tr&gt; &lt;!-- &lt;td&gt;&lt;%= link_to server.id, server_path(server) %&gt;&lt;/td&gt; --&gt; &lt;td&gt;&lt;%= server.hostname %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= server.port %&gt;&lt;/td&gt; &lt;!-- &lt;td&gt;&lt;%= server.username %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= server.password %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= server.ssh_username %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= server.ssh_password %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= server.source_branch %&gt;&lt;/td&gt; --&gt; &lt;td&gt;&lt;%= server.source_revision %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= server.release %&gt;&lt;/td&gt; &lt;td id="comments"&gt;&lt;%= server.rhel_version %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= server.gpu_type %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= server.total_users %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= server.current_users %&gt;&lt;/td&gt; &lt;td&gt;&lt;%=l server.created_at %&gt;&lt;/td&gt; &lt;td&gt; &lt;%= link_to t('.edit', :default =&gt; t("helpers.links.edit")), edit_server_path(server), :class =&gt; 'btn btn-mini' %&gt; &lt;%= link_to t('.destroy', :default =&gt; t("helpers.links.destroy")), server_path(server), :method =&gt; :delete, :confirm =&gt; t('.confirm', :default =&gt; t("helpers.links.confirm", :default =&gt; 'Are you sure?')), :class =&gt; 'btn btn-mini btn-danger' %&gt; &lt;/td&gt; &lt;/tr&gt; &lt;% end %&gt; &lt;/tbody&gt; &lt;/table&gt; &lt;%= link_to t('.new', :default =&gt; t("helpers.links.new")), new_server_path, :class =&gt; 'btn btn-primary' %&gt; </code></pre> <p><strong>/controllers/servers_controller.rb</strong></p> <pre><code>class ServersController &lt; 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 </code></pre> <p><strong>/assets/javascripts/application.js</strong></p> <pre><code>// 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 . </code></pre> <p><strong>Screenshot of view:</strong> <img src="https://i.stack.imgur.com/6KbtP.png" alt="statusboard view"></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload