2

がありますが、そのとアクションを 内からfriendships_controller呼び出したいと思います。実際、私が設定した方法では、メソッドは正常に機能しますが、機能しません。createdestroyusers_controllercreatedestroy

ユーザー/インデックス

<%= button_to "+ Add Friend", :controller => "friendships", :action => 'create', :method => "post", :id => user.id %>
<%= button_to "- Unfriend", {:controller => "friendships", :action => 'destroy'}, :confirm => "Are you sure you want to unfriend #{user.username}?", :method => :delete, :id => user.id %>

Unfriend ボタンをクリックすると、follow rails の例外が発生します。

ActiveRecord::RecordNotFound in FriendshipsController#destroy
Couldn't find User with ID=destroy

これは 内のdestroyアクションですfriendships_controller:

  def destroy
    @accepting_user = User.find(params[:id])
    @friendship = Friendship.find_by_accepting_user_id_and_requesting_user_id(@accepting_user.id, current_user.id)
    @friendship.destroy
    flash[:notice] = "You unfriended #{@friendship.accepting_user.username}."
    redirect_to(:back)
  end

誰でもこれについて何か考えがありますか?ありがとう。

アップデート

友好ルート:

rake routes | grep friendship
           friendships_index GET    /friendships/index(.:format)                                      {:controller=>"friendships", :action=>"index"}
                 friendships GET    /friendships(.:format)                                            {:controller=>"friendships", :action=>"index"}
                             POST   /friendships(.:format)                                            {:controller=>"friendships", :action=>"create"}
              new_friendship GET    /friendships/new(.:format)                                        {:controller=>"friendships", :action=>"new"}
             edit_friendship GET    /friendships/:id/edit(.:format)                                   {:controller=>"friendships", :action=>"edit"}
                  friendship GET    /friendships/:id(.:format)                                        {:controller=>"friendships", :action=>"show"}
                             PUT    /friendships/:id(.:format)                                        {:controller=>"friendships", :action=>"update"}
                             DELETE /friendships/:id(.:format)                                        {:controller=>"friendships", :action=>"destroy"}
4

1 に答える 1

6

うまくいきました、ちょうど私が通り過ぎていた場所に移動しなければなりませんでした:id

最終結果は次のようになります。

<%= button_to "- Unfriend", {:controller => "friendships", :action => 'destroy', :id => user.id}, :confirm => "Are you sure you want to unfriend #{user.username}?", :method => :delete %>
于 2011-11-11T02:29:41.933 に答える