Rails 从另一个控制器中调用 destroy 方法 [英] Rails call destroy method from within another controller

查看:46
本文介绍了Rails 从另一个控制器中调用 destroy 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a friendships_controller but I would like to call its create and destroy actions from inside the users_controller. Actually, the way I have things set up, the create method works fine, but destroy does not.

用户/索引

<%= 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 %>

如果我单击取消好友"按钮,则会出现跟随轨道异常:

If I click the Unfriend button I get the follow rails exception:

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

这是friendships_controller中的destroy动作:

  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

有人对此有什么想法吗?谢谢.

Anyone have any thoughts on this? Thanks.

更新

友谊路线:

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"}

推荐答案

成功了,只需要移动到我传入 :id

Got it working, just had to move where I was passing in the :id

最终结果如下:

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

这篇关于Rails 从另一个控制器中调用 destroy 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆