Rails: GoogleDocs autosave

I am trying to create a Rails application in which you can edit the same model from several different places on the page. I want the changes to be saved automatically every X seconds with the option of manually starting the save.

I came up with a solution, but it seems very complicated, and I believe that other Rails users have already faced similar questions.

The solution I came across is that there is a hidden form on my page that is actually presented, and then several β€œdummy” forms scattered around the page that update the hidden form.

After submitting, the hidden form updates the model, and the model contains logic to determine which RJS files should be returned in response. They are merged and sent as a response to the update.

some limitations:

  • It is impossible to wrap the entire page in one form tag (there are several models / controllers on the page).
  • The same field may be editable from several places.

Does anyone have a more efficient way?

+6
ajax ruby-on-rails webforms
source share
1 answer
  • create as many forms as you need on your page, even the same instance of the same model.
  • Starting the update will either be a javascript call to setInterval or onblur in your form fields.
  • your controller must be REST and it will return success or error messages in json variables and HTTP status (200, 422)
  • Forget rjs, think about client side. Each form on your page will submit the form to the update method of your controller. Javascript submitting the form will have an error or success callback, which will then show success or error messages. The idea is that the javascript submitting the form β€œknows” which forms it is currently submitting, and it should be able to show an error or success on its own, depending on the form it submits, this is not the job of the controller.
  • Saving the entire page simply serializes all the fields from all forms and sends them to the update method. (see serialize )
+3
source share

All Articles