2013-04-12 40 views
0

我試圖讓控制器的「銷燬」正常工作,我想知道正確的設置應該是什麼。ActiveRecord :: RecordNotFound在控制器#銷燬

的錯誤,我得到的是

ActiveRecord::RecordNotFound in AuthenticationsController#destroy 
Couldn't find Authentication without an ID 

我的控制器看起來像

class AuthenticationsController < InheritedResources::Base 
def destroy 
    @authentication = current_user.authentications.find(params[:id]) 
    @authentication.destroy 
    redirect_to(:back) 
end 

數據庫表

create_table "authentications", :force => true do |t| 
    t.integer "user_id" 
    t.string "provider" 
    t.string "uid" 
    t.string "secret" 
    t.string "token" 
    end 

我已經試過其他參數如:user_id說明 如何我可以讓用戶銷燬他們的令牌嗎? (與該選項後重新認證)

+0

聽起來像params [:id]工作不正常,請嘗試手動添加一個您知道是適當ID的數字,以查看它是否可以工作,例如, current_user.authentications.find(2) – Steve

+0

它工作時,我手動添加一個身份驗證ID,我該如何解決這個問題? –

+0

你如何執行請求?您的鏈接網址有問題,或者您正在使用銷燬方法。 –

回答

2

你不及格ID到控制器

嘗試

<%= link_to "Disconnect Your Authentication", {:controller=>'authentications', :action=>'destroy', :id=>current_user.authentication_id} %> 

或使用路徑傭工@autentication參數作爲選項。

(你需要編輯你的路由文件)

+0

路徑助手是要走的路 – patrickmcgraw

+0

毫米...當我需要傳遞一些額外的參數時,我經常使用第一個選項,這是不是好方法? –

+0

這只是我的看法。我認爲路徑助手可以使單元測試和重構更容易一些。但可能不足以說一種方法是好的,另一種方法是不好的。 – patrickmcgraw

0

如果你想消滅所有認證的用戶,你當然可以改變你的控制器的破壞方法是:

def destroy 
    current_user.authentications.destroy_all 
end 

一更傳統的方法是破壞特定的認證。在這種情況下,link_to方法需要一個包含一個id參數的路徑(這將最終成爲控制器中的params[:id]值)。你能想象一個視圖片段一樣顯示所有用戶的認證,每一個破壞的鏈接如下:

<ul> 
<% current_user.authentications.each do |a| %> 
    <li> 
    <%= a.provider %> 
    - 
    <%= link_to 'Disconnect Your Authentication', authentication_path(a), :method => :delete %> 
    </li> 
<% end %> 
</ul> 

這是假定CURRENT_USER是一個輔助和您的路由設置您的身份驗證模式。 authentication_path幫助程序使用a身份驗證實例生成一個路徑,並帶有一個id參數。