views:

230

answers:

2

I have a standard form in my Rails app.

In certain situations I'd like to display a Javascript confirmation dialog when the Submit button is clicked. The catch is that I need to determine whether or not to display this confirmation on the server side as opposed to doing this in Javascript before the request is submitted to the server.

What is the best solution to this?

Should I use AJAX and change my form_for tag to a remote_form_for tag? I will then be able to pick up a format.js response to display the confirmation dialog in my controller, but it does feel a bit messy.

Or should I just forget trying to do this with Javascript and just display an intermediate confirmation page?

A: 

I'm not savvy with Ruby on Rails, but the general flow between client and server would, in my opinion, be:

# Client request is >
# Server response is <
> /perform/action?arg1=abc
< {"perform":"action","confirmation_required":true}
> /perform/action?arg1=abc&confirm=1

So basically, make your script on the server simply return a response that tells the client it has to confirm the action and resend the request, then the client shows a dialog and if OK is pressed, adds a flag to the request that means the user has confirmed it.

Blixt
A: 

You could make your controller method set a flag in the flash to indicate whether the confirmation dialog should be displayed or not. Your view code would be conditional dependent on the presence of this flag.

John Topley
Thing is, I don't know whether or not to display the confirmation dialog until the form is submitted (i.e. it's based on the user data entered in the form).
Olly
If the form has already been submitted then how can you display the confirmation dialog when the Submit button has been clicked? It's already too late at that point.
John Topley