Topic: link_to with :method=>:delete not working ( Rails 3 )

Hey

I want to delete a record on my index view through a delete link.

In the view there is this code:

<%= link_to "Delete", admin_question_path(question), :confirm => "Are you sure?", :method=>:delete %>

the generated html looks like this:

<a href="/admin/questions/3" data-method="delete" rel="nofollow">Delete</a>

the link redirects to

http://localhost:3000/admin/questions/4/edit

in stead of

http://localhost:3000/admin/questions/4/destroy

in the routes.rb I've placed:

Questionator::Application.routes.draw do |map|

  root :to => "admin/dashboard#show"
  
  namespace "admin" do
    root :to => "admin/dashboard#show"
    resources :questions
  end
end
rake routes

contains this line:

admin_question DELETE /admin/questions/:id(.:format)      {:action=>"destroy", :controller=>"admin/questions"}

Can someone give me a clue where the problem might be at?

Re: link_to with :method=>:delete not working ( Rails 3 )

link_to with a method anything other than GET is actually a bad idea, as links can be right clicked and opened in a new tab/window, and because this just copies the url (and not the method) it will break for non-get links.   Also, links are clicked on by web page indexing spiders, and even though the links in question are probably only available to logged in users (and therefore not spiders) it's still bad practise.

It's better to use button_to instead, which makes rails generate a mini-form to produce the same result.
http://railsbrain.com/api/rails-2.3.2/d … =button_to

From a practical point of view buttons are better (for the above reasons) but they're also better from a conceptual point of view:  generally speaking, links should "take you somewhere", whereas buttons should "do something".  It's better to keep these two basic functionalities seperate.

###########################################
#If i've helped you then please recommend me at Working With Rails:
#http://www.workingwithrails.com/person/ … i-williams

Re: link_to with :method=>:delete not working ( Rails 3 )

Thanks for putting some basics into context.  Will try the button_to method. Preferred link_to to keep things in sync visually.

Re: link_to with :method=>:delete not working ( Rails 3 )

Ok, so I switched my code to work with the button_to helper seems to work, except for the confirmation popup.
I included :all javascripts and can see them when I view source.

Any thoughts?

Re: link_to with :method=>:delete not working ( Rails 3 )

webdevotion wrote:

Ok, so I switched my code to work with the button_to helper seems to work, except for the confirmation popup.
I included :all javascripts and can see them when I view source.

Any thoughts?

not unless you post your current code up...

###########################################
#If i've helped you then please recommend me at Working With Rails:
#http://www.workingwithrails.com/person/ … i-williams

Re: link_to with :method=>:delete not working ( Rails 3 )

btw here's a working example of a delete button (with confirm) from my code.  In case you've not used I18n before, the t("...") stuff is just calls to a translation table and could be replaced with hard coded strings for the purpose of this illustration:

button_to_remote t('general.delete'), :url => question_path(question), :method => :delete, :confirm => t('question.confirm_delete'), :title => t('question.delete_question')

###########################################
#If i've helped you then please recommend me at Working With Rails:
#http://www.workingwithrails.com/person/ … i-williams

Re: link_to with :method=>:delete not working ( Rails 3 )

oops sorry, that was a button_to_remote which has a slightly different syntax (path is the second argument rather than options[:url]).  here's the straight button_to version:

button_to t('general.delete'), :question_path(question), :method => :delete, :confirm => t('question.confirm_delete'), :title => t('question.delete_question')

Last edited by Max Williams (2010-04-19 10:39:42)

###########################################
#If i've helped you then please recommend me at Working With Rails:
#http://www.workingwithrails.com/person/ … i-williams

Re: link_to with :method=>:delete not working ( Rails 3 )

In my view:

<% if @question.id != nil %>
      <%= button_to "Delete", admin_question_path(@question.id), :method=>:delete, :class=>:destroy,:confirm=>"Are you sure?" %>
<% end %>

The generated HTML output:

<form method="post" action="/admin/questions/244"  class="button_to">
  <div>
    <input name="_method" type="hidden" value="delete" />
    <input class="destroy" data-confirm="Are you sure?" type="submit" value="Delete" />
    <input name="authenticity_token" type="hidden" value="yApLSCAIIqDBeubxR7RflOo+BnDByQHgSJzhtKvIATM=" />
  </div>
</form>

Included in my <head> tag:

<script src="/javascripts/prototype.js?1271659222" type="text/javascript"></script>
<script src="/javascripts/effects.js?1271659222" type="text/javascript"></script>
<script src="/javascripts/dragdrop.js?1271659222" type="text/javascript"></script>
<script src="/javascripts/controls.js?1271659222" type="text/javascript"></script>
<script src="/javascripts/rails.js?1271659222" type="text/javascript"></script>
<script src="/javascripts/application.js?1271659222" type="text/javascript"></script>

The record is actually deleted - so the only thing that doesn't work is the confirmation popup ...

Re: link_to with :method=>:delete not working ( Rails 3 )

hmm.  Mine looks like this but i'm using jrails which replaces generated prototype js with generated jquery js:

<form method="post" action="/question/144" class="button-to">
  <div>
    <input name="_method" type="hidden" value="delete" /><
    <input onclick="return confirm('Are you sure?');" title="Delete this question" type="submit" value="Delete" />
    <input name="authenticity_token" type="hidden" value="wybxZG3JozSBVln5NGn21E02jKv8ZZtZI6eHM5ER3o0=" />
  </div>
</form>
                
this is the difference and may be the cause of your problems:

data-confirm="Are you sure?"

I don't know why this is being generated, i've never seen this syntax for confirm functionality before.

###########################################
#If i've helped you then please recommend me at Working With Rails:
#http://www.workingwithrails.com/person/ … i-williams

Re: link_to with :method=>:delete not working ( Rails 3 )

Hey Max, thanks for your help, but looks like I found it.

This line was missing from my <head> section:

<%= csrf_meta_tag %>

To be complete now the <head> looks like:

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <title>Admin Questionnaire</title>
  <%= stylesheet_link_tag "reset" %>
  <%= stylesheet_link_tag "admin" %>
  <%= javascript_include_tag :all %>
  <%= csrf_meta_tag %>
</head>

PS: Max, like stated in the Topic Title, I'm using Rails 3 beta 3, so maybe that's the reason you haven't seen it before?

Re: link_to with :method=>:delete not working ( Rails 3 )

ah right, probably, yeah smile

i've not seen csrf_meta_tag either.  Does that 'switch on' the attributes like "data-confirm"?

###########################################
#If i've helped you then please recommend me at Working With Rails:
#http://www.workingwithrails.com/person/ … i-williams

Re: link_to with :method=>:delete not working ( Rails 3 )

Nice that you got something out of it : )
The whole point of helping each other, right?

csrf_meta_tag is a helper that returns two HTML tags. They include information necessary for Rails' new, beefy XSS support with good old protect_from_fogery. You will need these for Rails' Javascript to work and you will need it for security.

from http://www.themodestrubyist.com/2010/02 … meta-tags/

Re: link_to with :method=>:delete not working ( Rails 3 )

good to know for when i finally try rails 3 smile

cheers

###########################################
#If i've helped you then please recommend me at Working With Rails:
#http://www.workingwithrails.com/person/ … i-williams

Re: link_to with :method=>:delete not working ( Rails 3 )

I'll add this note for reference:

You will get the behavior that was described in the 1st post if you don't import the proper javascript files in your template.

<%= stylesheet_link_tag :all %>
<%= javascript_include_tag :defaults %>
<%= csrf_meta_tag %>

Re: link_to with :method=>:delete not working ( Rails 3 )

You have to include rails.js if you want to have behaviors like data-*.

Re: link_to with :method=>:delete not working ( Rails 3 )

I've included rails.js, as well as adding the meta tag.  Still doesn't work.

layout header looks like:

<head>

<script src="/javascripts/prototype.js?1296726881" type="text/javascript"></script>
<script src="/javascripts/effects.js?1296726878" type="text/javascript"></script>
<script src="/javascripts/dragdrop.js?1296726878" type="text/javascript"></script>
<script src="/javascripts/controls.js?1296726877" type="text/javascript"></script>
<script src="/javascripts/rails.js?1296726881" type="text/javascript"></script>
<script src="/javascripts/application.js?1296645895" type="text/javascript"></script>
<meta name="csrf-param" content="authenticity_token"/>
<meta name="csrf-token" content="H6uBz8uCoq7oOcewbmeqtC4UWhSX8eXWLh45H0cLMbY="/>
<script src="/javascripts/jquery-1.4.2.min.js?1296645895" type="text/javascript"></script>
    
</head>

Anything else it could be?

Re: link_to with :method=>:delete not working ( Rails 3 )

horseshoe7
pls provide us with more information

Re: link_to with :method=>:delete not working ( Rails 3 )

I had the same problem and then I found a post that mentioned something that helped. You must include the link to rails.js last. When I put the link to defaults after my link to jquery, the problem went away and it all started working properly.

Re: link_to with :method=>:delete not working ( Rails 3 )

Max Williams wrote:

From a practical point of view buttons are better (for the above reasons) but they're also better from a conceptual point of view:  generally speaking, links should "take you somewhere", whereas buttons should "do something".  It's better to keep these two basic functionalities seperate.

Good advice. I never thought of that. smile

csrf_meta_tag is somewhat explained here. Tag lines "Just do it" and "It just works" come to mind. lol
http://www.themodestrubyist.com/2010/02 … meta-tags/

Learning rails, one day at a time.

http://keilmiller.com

Re: link_to with :method=>:delete not working ( Rails 3 )

Try remove jquery smile))))))))))))))))))
jquery override prototype's $(). For sure, your rails.js is for prototype and is accessing jquery by calling $().

So:

* remove jquery's include
* keep jquery, remove prototype's include and swith rails.js to rails-jquery.js