2

I have a rails app, some pages show data from DB, does any way that when I change specify DB record(almost use phone app calls web API),and if my web idle on page which related the change data, the specify page will auto reload or refresh? does it can use ajax or javascript to achieve?

I use rails 4.2.2 & mysql

Zombo
  • 1
  • 62
  • 391
  • 407
John
  • 387
  • 5
  • 17

1 Answers1

0

Inside your model, use the following code:

class MyModel
  after_update :reload_page

  def reload_page
    ### Code to reload page
  end
end

The after_update attribute means whenever a record changes(changes committed), the reload_page method will be called.

Also, you might take a look at this post that shows how to refresh a page.

In case you want to use an API or do the same thing in a controller, do:

class MyController
  after_filter :reload_page, :only => :update

  def reload_page
    ### Code to reload page
  end
end

That way, whenever MyController#updatemethod is called, reload_page will be executed.

Community
  • 1
  • 1
Omar Lahlou
  • 1,000
  • 9
  • 33
  • but I use API to change data, how do I set the reload_page? if my website have have pageA, I want to refresh only pageA when data change(there are other method will change other data columns) – John Aug 19 '15 at 03:06