Rails从另一个控制器内调用destroy方法

Ton*_*ate 2 destroy ruby-on-rails-3

我有一个,friendships_controller但我想从内部调用它createdestroy行动users_controller.实际上,我设置的方式,create方法工作正常,但destroy没有.

用户/指数

<%= 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 %>
Run Code Online (Sandbox Code Playgroud)

如果我单击Unfriend按钮,我会得到跟踪轨道异常:

ActiveRecord::RecordNotFound in FriendshipsController#destroy
Couldn't find User with ID=destroy
Run Code Online (Sandbox Code Playgroud)

这是以下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
Run Code Online (Sandbox Code Playgroud)

有没有人对此有任何想法?谢谢.

UPDATE

友谊路线:

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"}
Run Code Online (Sandbox Code Playgroud)

Ton*_*ate 6

得到它的工作,只需要移动到我传递的地方 :id

最终结果如下:

<%= button_to "- Unfriend", {:controller => "friendships", :action => 'destroy', :id => user.id}, :confirm => "Are you sure you want to unfriend #{user.username}?", :method => :delete %>
Run Code Online (Sandbox Code Playgroud)