Google Mail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Adding a delete confirmation
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  8 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Follow-up To:
Add Cc | Add Follow-up to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers that you hear
 
KJoyner  
View profile   Translate to Translated (View Original)
 More options 25 Jan 2008, 01:19
From: KJoyner <K...@kjoyner.com>
Date: Thu, 24 Jan 2008 17:19:11 -0800 (PST)
Local: Fri 25 Jan 2008 01:19
Subject: Adding a delete confirmation
I am trying to use Low Pro to add a delete confirmation. In my
controller, I have added a delete and a destroy action. These content
of these actions are:

def delete
  @item = Item.find( params[ :id ] )
end

def destroy

  @item = Item.find( params[ :id ] )
  item.destroy

  redirect_to items_path
end

In the routes, I add a delete route to items by:

map.resources :items, :member => { :delete => :get }

I then have a view where I add a delete link like this (also using
haml):

= link_to "delete", delete_item_path( item ), :class => 'delete'

All of this works when I have javascript disabled. Now I am trying to
add a behavior that will do the confirmation in javascript and if
confirmed then redirect to the destroy action. I am trying to do this
as follows but it is not working.

Remote.Delete = Behavior.create( Remote.Base, {
  onclick: function( event ) {
    if ( confirm( 'Are you sure?'  ) ) {
      var destroy_url = this.element.href.gsub( '/delete$', '' );
      options = Object.extend( { url: destroy_url, method: 'delete' },
this.options );
      return this._makeRequest( options )
    }
    return false;
  }

});

Event.addBehavior( {
  '.delete' : Remote.Delete

});

The onclick gets executed but it is not working correctly. Any help
would be appriciated.

Thanks,

-- Ken


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Matt Aimonetti  
View profile   Translate to Translated (View Original)
 More options 25 Jan 2008, 01:34
From: "Matt Aimonetti" <mattaimone...@gmail.com>
Date: Thu, 24 Jan 2008 17:34:54 -0800
Local: Fri 25 Jan 2008 01:34
Subject: Re: Adding a delete confirmation

Remote.DeleteLink = Behavior.create(Remote.Base, {
  onclick : function() {
    var options = Object.extend({ url : this.element.href, method : 'delete'

}, this.options);

    if (confirm('Are you sure?')){
      return this._makeRequest(options);
    }else{
      return false;
    }
  }

});

don't change your route, a delete action should not be a GET but a DELETE.

I'm sure my code could be cleaned up by extending Remote.Link but oh well,
this was a quick hack.

-Matt

On 1/24/08, KJoyner <K...@kjoyner.com> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jarkko Laine  
View profile   Translate to Translated (View Original)
 More options 25 Jan 2008, 05:40
From: Jarkko Laine <jar...@jlaine.net>
Date: Fri, 25 Jan 2008 07:40:26 +0200
Local: Fri 25 Jan 2008 05:40
Subject: Re: Adding a delete confirmation
On 25.1.2008, at 3.34, Matt Aimonetti wrote:

> Remote.DeleteLink = Behavior.create(Remote.Base, {
>   onclick : function() {
>     var options = Object.extend({ url : this.element.href, method :  
> 'delete' }, this.options);
>     if (confirm('Are you sure?')){
>       return this._makeRequest(options);
>     }else{
>       return false;
>     }
>   }
> });

> don't change your route, a delete action should not be a GET but a  
> DELETE.

I think he's using the delete action (note the name) as an  
intermediary action to hold a form with DELETE method pointing to the  
_destroy_ action that actually deletes the record. That's pretty much  
the only way to use an accessible link to perform a destructive action  
afaik.

So what happens? Does the request get to Rails? Have you used Firebug  
to debug what happens inside the behaviour (e.g. what does options  
look like when you make the request)?

--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
KJoyner  
View profile   Translate to Translated (View Original)
 More options 25 Jan 2008, 06:05
From: KJoyner <K...@kjoyner.com>
Date: Thu, 24 Jan 2008 22:05:27 -0800 (PST)
Local: Fri 25 Jan 2008 06:05
Subject: Re: Adding a delete confirmation
Jarkko is correct, I am trying to create both an accessible link (when
Javascript is disabled) and an AJAX request when java is enabled.

After reading your post, I just thought to check my log to see if it
showed anything. It does route but shows an
ActionController::InvalidAuthenticityToken during a call to
request_forgery_protection.

On 24 Jan, 21:40, Jarkko Laine <jar...@jlaine.net> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jarkko Laine  
View profile   Translate to Translated (View Original)
 More options 25 Jan 2008, 07:05
From: Jarkko Laine <jar...@jlaine.net>
Date: Fri, 25 Jan 2008 09:05:20 +0200
Local: Fri 25 Jan 2008 07:05
Subject: Re: Adding a delete confirmation

On 25.1.2008, at 8.05, KJoyner wrote:

> Jarkko is correct, I am trying to create both an accessible link (when
> Javascript is disabled) and an AJAX request when java is enabled.

> After reading your post, I just thought to check my log to see if it
> showed anything. It does route but shows an
> ActionController::InvalidAuthenticityToken during a call to
> request_forgery_protection.

This is the new csrf killer in action :-)

You must pass the authenticity token to the link somehow (see http://edgedocs.planetargon.org/classes/ActionController/RequestForge...)
. You could for example set a header for it in the controller and then  
set the form parameter in your behaviour according to that header.

See http://edgedocs.planetargon.org/classes/ActionView/Helpers/UrlHelper....
  (click "Source") for how the Rails JS helpers populate the token in  
link_to_remote.

--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
KJoyner  
View profile   Translate to Translated (View Original)
 More options 25 Jan 2008, 07:20
From: KJoyner <K...@kjoyner.com>
Date: Thu, 24 Jan 2008 23:20:26 -0800 (PST)
Local: Fri 25 Jan 2008 07:20
Subject: Re: Adding a delete confirmation
I have added the following that now works, except for the reload which
does not appear to be working as I intended. For completeness, I have
included my updated delete method.

var AJ =
{
  encode_authenticity_token:function( token )
  {
      return encodeURIComponent( $(token).value )
  },

  authenticity_token_query_parameter_for_page:function()
  {
      return 'authenticity_token=' + AJ.encode_authenticity_token(
        document.body.select( 'input[ name = "authenticity_token" ]' )
[ 0 ] )
  }

}

Remote.Delete = Behavior.create( Remote.Base,
{
  onclick: function( event )
  {
      if ( confirm( 'Are you sure?' ) )
      {
        var options =
        {
            url:        this.element.href.gsub( '/delete$', '' ),
            method:     'delete',
            parameters:
AJ.authenticity_token_query_parameter_for_page()
        };
        options = Object.extend( options, this.options );

        this._makeRequest( options );
        Event.addBehavior.reload();
      }
      return false;
  }

});

  def destroy
    item = Item.find( params[ :id ] )
    item.destroy

    respond_to do |wants|
      wants.html do
        redirect_to item_path
      end
      wants.js do
          @items = Item.find(:all)
          render
      end
    end
  end

On 24 Jan, 23:05, Jarkko Laine <jar...@jlaine.net> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jarkko Laine  
View profile   Translate to Translated (View Original)
 More options 25 Jan 2008, 08:25
From: Jarkko Laine <jar...@jlaine.net>
Date: Fri, 25 Jan 2008 10:25:16 +0200
Local: Fri 25 Jan 2008 08:25
Subject: Re: Adding a delete confirmation

On 25.1.2008, at 9.20, KJoyner wrote:

> I have added the following that now works, except for the reload which
> does not appear to be working as I intended. For completeness, I have
> included my updated delete method.

> var AJ =
> {
>  encode_authenticity_token:function( token )
>  {
>      return encodeURIComponent( $(token).value )

Can't answer to your reload question off the top of my head, but you  
can replace $(token).value with $F(token) (see http://prototypejs.org/api/utility/dollar-f)

>        document.body.select( 'input[ name = "authenticity_token" ]' )
> [ 0 ] )

This is also same as $$('input[name="authenticity_token']).first() (http://prototypejs.org/api/utility/dollar-dollar
). If you give the input field an id, it can be reduced to $
('authenticity_token').

--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
KJoyner  
View profile   Translate to Translated (View Original)
 More options 26 Jan 2008, 00:57
From: KJoyner <K...@kjoyner.com>
Date: Fri, 25 Jan 2008 16:57:35 -0800 (PST)
Local: Sat 26 Jan 2008 00:57
Subject: Re: Adding a delete confirmation
I changed the auth code as follows but cannot figure out any way to
furthur simplify.

var AJ =
{
  encode_authenticity_token:function( token )
  {
      return encodeURIComponent( $F(token) )
  },

  authenticity_token_query_parameter_for_page:function()
  {
      return 'authenticity_token=' + AJ.encode_authenticity_token(
        $$( 'input[ name = "authenticity_token" ]' ).first() )
  }

}

I also moved to event delegation instead of trying to make the reload
work. The javascript now looks as follows:

Remote.Delete = Behavior.create( Remote.Base,
{
  onclick: function( event )
  {
    var element = Event.element( event );
    if ( element.hasClassName( 'delete_link' ) )
    {
      if ( confirm( 'Are you sure?' ) )
      {
        var options =
        {
            url:        element.href.gsub( '/delete$', '' ),
            method:     'delete',
            parameters:
AJ.authenticity_token_query_parameter_for_page()
        };
        options = Object.extend( options, this.options );

        this._makeRequest( options );
      }
      return false;
    }
    return true;
  }

});

Event.addBehavior(
{
  '#item_list' : Remote.Delete

});

Notice, that this attaches a behavior to a div with an ID of
item_list. If the element that received the click has a classname of
delete_link, then a confirmation will be displayed and the element
deleted through the destroy method.

On 25 Jan, 00:25, Jarkko Laine <jar...@jlaine.net> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google