0

There are a number of queries in this area, but I've yet to find a concrete answer.

I have built a rails app using rails 4. I have a model and controller called 'VirtualServer'

The VirtualServer model stores the name, id, and other attributes about a real virtual machine.

When I call the VirtualServer show action, it renders the show.html.erb

Now I want to dynamically update some information on this page using AJAX. Specifically I want to access some runtime data about the virtual machine that is available from a RESTful API. I have a method in my VirtualServer called stats that uses a 3rd party client to make this request, and it returns data in a hash.

After rendering show, I'd like to be able to poll stats every 10 seconds and render a partial.

Is there an inbuilt way in rails 4, to make ajax requests from a view at a regular interval and render a partial that displays the info?

virtual_server_controller.rb

class VirtualServer < ApplicationController
  def show
    @virtualServer = VirtualServer.find(params[:id])
  end

  def stats
     vm_id = params[:id]
     # Thirdparty code make RESTful request with vm_id
     # and return hash
     #
     @vm_details = client.get_vm_stats(vm_id)
  end
end

virtual_server/show.html.erb

<p>
    <strong>Name:</strong>
    <%= @vnf.name %>
</p>

<!-- need to call VirtualServer.stats and render result every 10 sec -->
Dave
  • 656
  • 2
  • 6
  • 22
  • I've recently explained how to render a partial into an exisitng view using ajax here http://stackoverflow.com/questions/32651676/avoiding-layout-reloading-in-ruby-on-rails-project/32652005#32652005 which may help – Scott Oct 14 '15 at 14:32
  • Thanks, I worked through the first post, which almost worked perfectly. The problem is when I add my virtual_server.js to assets/javacsripts, it gets loaded for every page. Is there a way to make it apply only to a specific controller ? – Dave Oct 14 '15 at 14:48

0 Answers0