2016-12-17 6 views
0

URL application.co/users/2には、current_useruser 2の関係を削除するためのフォームがあります。フォームのコードは次のとおりgetとは異なるリクエストのparamsハッシュ

<%= form_for(current_user.active_relationships.find_by(followed_id: @user.id), html: { method: :delete }) do |f| %> 
    <%= f.submit "Unfollow", class: "btn" %> 
<% end %> 

関係コントローラの破壊作用がある:

def destroy 
    user = Relationship.find(params[:id]).followed 
    current_user.unfollow(user) 
    redirect_to user 
    end 

なぜアクションがparams[:id]は関係のIDを表すという仮定に依存していますか?私はparams[:id]がURLの2番を表していると思った。
Michael Hartlのチュートリアルでlink1link2を参照してください。

答えて

1

form_forRelationshipクラスのオブジェクトで初期化するので、Userクラスではありません。

そこは

current_user.active_relationships.find_by(followed_id: @user.id)

Relationshipオブジェクトを返します
関連する問題