views:

26

answers:

1

I am trying to convert my Rails 2 app to Rails 3, but I can't delete any resources using my old Rails 2 code. To be precise I am trying to delete a resource, using this link:

<%= link_to image_tag("misc/delete.png"), @book, :confirm => 'Are you sure?', :method => :delete %>

And yet it doesn't work at all! It just behaves as if the :confirm option and :method option haven't been set at all, i.e. redirects me to the url of the @book object without even showing an alert box.

The generated HTML in Rails 3 is:

<a href="/books/13" data-method="delete" rel="nofollow"><img alt="Delete" src="/images/misc/delete.png?1205252772"></a>

The generated HTML in Rails 2 was:

<a href="/books/11" class="small" onclick="if (confirm('Are you sure?')) { var f = document.createElement('form'); f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;var m = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method'); m.setAttribute('value', 'delete'); f.appendChild(m);var s = document.createElement('input'); s.setAttribute('type', 'hidden'); s.setAttribute('name', 'authenticity_token'); s.setAttribute('value', 'uPeQI9FZxJT+DQlWvb02X5FEihG/hJgBk+vUhDwYT8o='); f.appendChild(s);f.submit(); };return false;"><img alt="Delete" src="/images/misc/delete.png?1279402305"></a> 

It's an obvious difference, but I've got no idea how I should handle this problem.

My controller looks like so:

class BooksController < ApplicationController
  before_filter :require_admin, :only => ['new', 'create', 'edit', 'update', 'destroy']

  # ....

  def destroy
    puts "-------------- DESTROYING BOOK --------------"
    @book = Book.find(params[:id])
    @book.destroy

    flash[:notice] = "Successfully destroyed book."
    session[:restore] = request.referer
    redirect_to back(edit_author_url(@book.author))
  end
end

And the string "destroying book" doesn't show on the console, so I think there surely must be something wrong.

Has something in the restful handling been changed in Rails 3 that I should get to know of?

Thanks, guys!

+1  A: 

You need add the rails javascript library.

shingara
Yes, it's something like that. It didn't appear to need that in Rails 2. I've forgotton to add the Rails-3-specific *rails.js*, but it's giving me errors in Chrome. I'll see what I can do. Thanks!
Albus Dumbledore
so accept the answer :)
shingara
Sure ;-) But first I need to make it work and be sure that's it. I'm now having trouble with prototype.js: `Uncaught TypeError: Object #<an Object> has no method 'dispatchEvent'`
Albus Dumbledore
Problem solved. JQuery was overwritting the Prototype's `$` method. Funny stuff.
Albus Dumbledore
FYI for anyone else with a similar problem how to stop the overwriting, you can use use "noconflict"
dryprogrammers
Yes, exactly: http://docs.jquery.com/Using_jQuery_with_Other_Libraries Thanks all!
Albus Dumbledore