简体   繁体   English

在Rails 3中管理宁静的资源

[英]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. 我正在尝试将Rails 2应用程序转换为Rails 3,但是无法使用旧的Rails 2代码删除任何资源。 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, ie redirects me to the url of the @book object without even showing an alert box. 它的行为就像完全没有设置:confirm选项和:method选项一样,即,将我重定向到@book对象的url,甚至不显示警告框。

The generated HTML in Rails 3 is: 在Rails 3中生成的HTML为:

<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: 在Rails 2中生成的HTML是:

<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. 而且字符串“ destroying book”没有显示在控制台上,因此我认为肯定有问题。

Has something in the restful handling been changed in Rails 3 that I should get to know of? 我应该了解在Rails 3中对静态处理的某些更改吗?

Thanks, guys! 多谢你们!

You need add the rails javascript library. 您需要添加rails javascript库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM