How to make confirm boxes conditional?

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

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

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

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

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 :slight_smile:

Best regards

Peter De Berdt