1

コントローラーの「破壊」を正しく機能させようとしていますが、正しいセットアップはどうあるべきか疑問に思っています。

私が得ているエラーは

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 などの他のパラメーターを試しました。ユーザーにトークンを破棄させるにはどうすればよいですか? (後で再認証するオプション付き)

4

2 に答える 2

2

IDをコントローラーに渡していません

試す

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

または @autentication 引数をオプションとしてパス ヘルパーを使用します。

(ルートファイルを編集する必要があります)

于 2013-04-12T23:52:13.773 に答える
0

ユーザーのすべての認証を破棄したい場合は、コントローラーの destroy メソッドを次のように変更できます。

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 パラメータを追加します。

于 2013-04-13T00:04:01.730 に答える