views:

239

answers:

1

Trying to make a custom :confirm message for a rails form that returns data from the submitted form--not just a static string.

<% form_for @foo do |f| -%>
<% f.text_field :number_of_bars -%>
<% f.submit :confirm => Are you really sure you want to use ##number_of_bars## bars? -%>

The idea is if the user puts in the number 3 in the number of bars text field the confirm message would show up like this: "Are you really sure you want to use 3 bars?"

Any ideas how to do this?

+1  A: 

Try this:

<%= f.submit :onclick => "return confirm('Are you really sure you want to use '+
          document.getElementById('number_of_bars_id').value + '?' )" %>

Replace the number_of_bars_id with the id of your field.

If you google a bit you will find a way for this to work with :confirm.

KandadaBoggu
This isn't going to work... you need to use javascript.
jonnii
Tried it but I got 0, which is the original value for that field...
Kenji Crosland
Which JS library are you using?
jonnii
just the standard prototype. My javascript skills are not so hot so I tried to stay away from it as much as possible.
Kenji Crosland
I didn't realize you need the value in form (it should have been obvious to me). I have updated the answer. I am sure there better ways to do this.
KandadaBoggu
hmm, not quite working. I'll see if google helps me
Kenji Crosland
I tested the code and it works for me. You have to replace the `number_of_bars_id` with the correct HTML element id for the `:number_of_bars` field.
KandadaBoggu
yeah, did that, not so sure why it's not working. It just submits without any confirm box coming up.
Kenji Crosland
Got it to work, misplaced quotation mark! Thanks!
Kenji Crosland
yes. I have fixed the answer.
KandadaBoggu