Reputation: 3574
I know it's a difficult task, but is there any good way to save form input when the user navigates to a different page without submitting, so when they come back they won't have to re-enter the info?
I'm using Rails 4, Simple Form, and jQuery, but I'm open to any solution that isn't an ugly hack.
Upvotes: 1
Views: 1012
Reputation: 3574
These are all great ideas, but I've just found the perfect solution for me: Garlic.js.
For anyone with a similar problem in the future, all you have to do with garlicjs is include the library and add the following to your form tag:
data-persist="garlic"
The library pretty much takes care of everything else from there exactly as you would expect. If you don't like some of it's presets, it seems pretty well documented and almost every featured seems to be disableable.
One problem is that it is an HTML5 only solution, but that's fine for my purposes. If you need something more cross compatible, Rob's solution looks good.
Upvotes: 3
Reputation: 8954
The jQuery serialize function may be your friend here. You could serialize on the way out and save it to DB or localStorage and then do the reverse when they come back to the page.
http://api.jquery.com/serialize/
Alternatively you could loop through the elements and store them in an array/object which you then also store in localStorage and on re-entry you re-populate the values of the form accordingly.
Upvotes: 6
Reputation: 11499
If you don't want any external dependencies, you can just make a back button (or whatever the other page is) that's actually submitting the form. Then just put in an if
statement based on the value of params[:submit]
and use the params
hash to populate some hidden fields on the new page.
When you click back to the original form, the hidden fields get submitted and populate the inputs.
Upvotes: 1