2010-10-20 54 views
0

我想將我的Rails 2應用程序轉換爲Rails 3,但我無法使用舊的Rails 2代碼刪除任何資源。準確地說,我試圖刪除一個資源,使用此鏈接:在Rails 3中管理restful資源

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

然而它根本不起作用!它的行爲就好像:confirm選項和:method選項一樣沒有設置,即將我重定向到@book對象的url,甚至沒有顯示警告框。

在Rails 3中生成的HTML是:

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

在梁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> 

這是一個明顯的區別,但我還是不知道我應該如何處理這個問題。

我的控制器看起來像這樣:

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 

和字符串「毀滅書」不顯示在控制檯上,所以我覺得有想必一定是有些不妥。

在我應該知道的Rails 3中,有沒有在restful處理中改變過的東西?

謝謝,夥計們!

回答

1

您需要添加rails JavaScript庫。

+0

是的,這是類似的東西。它似乎並不需要在Rails 2中。我忘了添加Rails-3特定的* rails.js *,但它給我在Chrome中的錯誤。我會看看我能做什麼。謝謝! – 2010-10-20 17:28:12

+0

當然;-)但首先我需要使它工作,並確保就是這樣。我現在遇到了prototype.js的問題:'Uncaught TypeError:Object# has no method'dispatchEvent'' – 2010-10-20 18:16:08

+0

問題解決了。 JQuery覆蓋了Prototype的'''方法。有趣的事。 – 2010-10-20 18:22:21