POST when leaving page

I’m on my first Rails app, and have this question.

I have a page with a form and with next and previous buttons (which
are do submit/post), and some other links (like logout, update-user-
profile).

Problem – I want it to look to the user like, as soon as they enter
something in a field, it’s posted and in the database (barring unusual
occurrences).

One way I can imagine is for the form to be automagically posted no
matter how the user leaves the page, by the submit buttons or the
links, or even, if possible, the user navigating away by entering a
new url. Does this require javascript, and if so, how do I do it?
I’ve certainly seen plenty of web pages that know when you are
navigating away by any means.

Another way, it seems to me not as good, is simply to make the links
into submit buttons. That’s fine, but can I at least style a button
to look like a link? This and subsequent methods would, I presume,
not save if the user navigated away without my buttons or links.

Or, is there a way to make a link do a post?

Or, is there some way to do it with routes (I just started reading
about them)?

Or, something else altogether? (Sorry for my ignorance on what might
be a simple question.)

Thanks. --David.

there are several ways to do such things.

you can call the submit function with javascript, whenever somebody uses
a link.

you can somehow do this on the document onunload event too, if the user
navigates away (but i don’t think that’s good practice)

make the links to submit buttons is a bad idea, because all actions that
are linked would have to be able to handle the data they receive.

maybe observe_field would be an option, you could post all changes in
form fields whenever those change:
http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper.html#M000966

i would most likely go for this option if i wanted to do something like
this (but i wouldn’t really want to, in most cases a simple form with a
simple submit should be enough)

Thanks. I am trying to avoid js for now – just starting rails and I
have enough on my hands. I tried onunload in the w3schools tryit
editor. It worked, but when I combined it with a form, it did not
work. Any ideas?

Since the only non-js solution you propose is making links into
submits, I lean to that. I am not worried about processing the data
and redirecting afterwards – the controller is already doing all
that, since I have [previous] and [next] submit buttons. Does this
make sense?

Also, can I style a submit button to at least look more like a link?

Thanks. --David

On May 6, 1:18 pm, Thorsten M. [email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs