Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can we circumvent these remote forms drawback?

In an effort to have everything translateable in our website ( including the error messages for the validations ), we switched almost all of our forms to remote forms. While this helps with the ability to translate error messages, we have encountered other problems, like:

  • if the user clicks on the submit button multiple times, the action gets called multiple times. If we have a remote form for creating a new record in the database, and assuming that the user's data is valid, each click will add a new object ( with the exact same contents ). Is there any way of making sure that such things cannot happen?

Is there somewhere I could read about remote forms best practices? How could I handle the multiple clicks problem? Is switching all the forms to remote forms a very big mistake?

like image 695
Geo Avatar asked Dec 21 '22 20:12

Geo


2 Answers

There is a rails 3 option called :disable_with. Put this on input elements to disable and re-label them while a remote form is being submitted. It adds a data-disable-with tag to those inputs and rails.js can select and bind this functionality.

submit_tag "Complete sale", :disable_with => "Please wait..."

More info can be found here

like image 200
Dty Avatar answered Dec 31 '22 01:12

Dty


Easy, and you can achieve that in many ways depending your preferences:

  1. Post the form manually simply using an ajax request and while you wait for the response disable/hide (or whatever you need) the form to ensure the user can't keep doing posts as crazy. Once you get the response from the server, again you can allow the user to post again (cleaning the form first), or show something else or redirect it to another page or again whatever you need.

  2. Use link_to :remote=>true to submit the form and add a callback function to handle the response and also to disable/hide (or whatever you need) the form when it's submitted

  3. Add a js listener to the form to detect when it's submitted and then disable/hide/whatever the form

As you see, there are lots of different ways to achieve what you need.

EDIT: If you need info about binding or handling a form submit from js here you'll find very easy and interesting examples that may help you to do what I suggested you! jQuery Submit

like image 27
FedeX Avatar answered Dec 30 '22 23:12

FedeX