how to make confirm boxes conditional?

577 views
Skip to first unread message

Lille

unread,
Jul 6, 2010, 8:47:49 AM7/6/10
to Ruby on Rails: Talk
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

Peter De Berdt

unread,
Jul 6, 2010, 10:23:43 AM7/6/10
to rubyonra...@googlegroups.com
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


Lille

unread,
Jul 6, 2010, 12:09:12 PM7/6/10
to Ruby on Rails: Talk
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

Peter De Berdt

unread,
Jul 6, 2010, 12:26:36 PM7/6/10
to rubyonra...@googlegroups.com

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

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.

Sure, but inline javascript like that is just a horrible thing :-)

Bob Proulx

unread,
Jul 6, 2010, 5:56:50 PM7/6/10
to Ruby on Rails: Talk
Lille wrote:
> 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.

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

Reply all
Reply to author
Forward
0 new messages