Search code examples
ruby-on-railsrubycontrollersaveautosave

How to allow a user to save a model without publishing it - ruby on rails


What would be the process of allowing a user to begin creating an object (foo) but mid-way the user would like to save foo so that they may be able to return to it later. How would I do this? The only way I know of "saving" an object is by doing just that, by sending it to the create action in the controller and creating the object.

def create
  @foo = current_user.foo.create(foo_params)
end

But how would I allow a user to return to foo and finish what they started so that they may be able to publish foo to be viewable at the foo index.

I would eventually like to create an "autosave" functionality so that if the user closes the window before pressing save that it will still be accessible.


Solution

  • One way would be to have an active column which is set to true when the object is created/completed by the user. The foo index would then only display active objects. In the form you could have an autosave feature using JS/AJAX that could periodically save the form in the background:

    autoSave = ->
      $.ajax
        type: 'POST'
        data: $("#form").serialize()
        url: '/foo/autosave'
        dataType: 'script'
      return
    
    $(document).ready ->
      setTimeout autoSave, 10000
      return