# How to make confirm boxes conditional?

**URL:** <https://www.ruby-forum.com/t/how-to-make-confirm-boxes-conditional/191148>\
**Category:** Rails\
**Created:** [July 6, 2010, 2:49pm UTC](https://www.ruby-forum.com/t/how-to-make-confirm-boxes-conditional/191148 "2010-07-06T14:49:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f4b2a3/32.png) [@Lille](https://www.ruby-forum.com/u/Lille)\
**Post date:** [July 6, 2010, 2:49pm UTC](https://www.ruby-forum.com/t/how-to-make-confirm-boxes-conditional/191148/1 "2010-07-06T14:49:06Z")

</div>

Hi,

I’d like to make the :confirm option in my link\_to/button\_to erb  
elements conditional. So, if the page is in some state, no  
confirmation is requested, but not otherwise, etc.

So far, my only (ugly) approach is to replace the linker html itself  
depending on changes in state, but I believe there must be an easier  
way to ‘toggle’ the confirmation option on/off.

Can anyone recommend an approach?

Thanks,

Lille

---

<div class="post-metadata">

**Author:** ![Lille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f4b2a3/32.png) [@Lille](https://www.ruby-forum.com/u/Lille)\
**Post date:** [July 6, 2010, 4:25pm UTC](https://www.ruby-forum.com/t/how-to-make-confirm-boxes-conditional/191148/2 "2010-07-06T16:25:09Z")

</div>

On 06 Jul 2010, at 14:47, Lille wrote:

> I’d like to make the :confirm option in my link\_to/button\_to erb  
> elements conditional. So, if the page is in some state, no  
> confirmation is requested, but not otherwise, etc.
> 
> So far, my only (ugly) approach is to replace the linker html itself  
> depending on changes in state, but I believe there must be an easier  
> way to ‘toggle’ the confirmation option on/off.
> 
> Can anyone recommend an approach?

Write your own Javascript which handles these page state changes and  
confirmation instead of relying on Rails Javascript helpers. Those  
helpers were only made for the most basic operations. You wouldn’t use  
a teaspoon to empty a bath either, even if you could put a bigger  
handle on the spoon.

Flow:

- Observe link clicks (you can add a class=“confirmable” or something  
to them if you want to apply it only to that element)
- Observe the elements on the page that will trigger a state change
- If those elements are changed, add the state to window or something
- When a link is clicked, it should see if window.state\_changed ==  
true and display the confirmation, if not, just window.location ==  
link.href

You would be best off implementing this in an event delegated way if  
there’s a lot of elements that can change the page state, as well as  
new elements being injected into the page via AJAX.

Best regards

Peter De Berdt

---

<div class="post-metadata">

**Author:** ![Lille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f4b2a3/32.png) [@Lille](https://www.ruby-forum.com/u/Lille)\
**Post date:** [July 6, 2010, 6:10pm UTC](https://www.ruby-forum.com/t/how-to-make-confirm-boxes-conditional/191148/3 "2010-07-06T18:10:33Z")

</div>

Peter,

Actually, the matter is simpler than either of us seemed to  
anticipate: the :confirm helper option just isn’t very helpful. In  
other words, all :confirm=\>‘Are you sure?’ does is produce the raw  
html onclick=\>“return confirm(‘Are you sure?’)”. I had assumed that if  
it were a helper, I should want to use it, not duplicate it, ergo my  
post.

So, if anyone ever wants to make conditional the :confirm option of  
the link\_to/button\_to erb function, use :onclick=\>“if(…)  
{ confirm(‘whatever text’);}” – involving whatever conditional logic  
you like in the raw javascript.

Lille

---

<div class="post-metadata">

**Author:** ![Lille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f4b2a3/32.png) [@Lille](https://www.ruby-forum.com/u/Lille)\
**Post date:** [July 6, 2010, 11:58pm UTC](https://www.ruby-forum.com/t/how-to-make-confirm-boxes-conditional/191148/4 "2010-07-06T23:58:19Z")

</div>

Lille wrote:

> you like in the raw javascript.  
> And what are your plans if the user’s browser isn’t running  
> Javascript? Because in that case they won’t ever see your  
> confirmation. Progressive Enhancement is better than Graceful  
> Degradation.

Bob

---

<div class="post-metadata">

**Author:** ![Lille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f4b2a3/32.png) [@Lille](https://www.ruby-forum.com/u/Lille)\
**Post date:** [July 6, 2010, 6:28pm UTC](https://www.ruby-forum.com/t/how-to-make-confirm-boxes-conditional/191148/5 "2010-07-06T18:28:08Z")

</div>

On 06 Jul 2010, at 18:09, Lille wrote:

> you like in the raw javascript.  
> Sure, but inline javascript like that is just a horrible thing 🙂

Best regards

Peter De Berdt
