Managing restful resources in Rails 3
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 :confir开发者_运维技巧m
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!
You need add the rails javascript library.
- To jQuery : http://github.com/rails/jquery-ujs
- To Prototype : http://github.com/rails/prototype-ujs
精彩评论