gpt4 book ai didi

ruby-on-rails-3 - Rails 从另一个 Controller 中调用 destroy 方法

转载 作者:行者123 更新时间:2023-12-02 23:59:37 26 4
gpt4 key购买 nike

我有一个 friendships_controller,但我想从 users_controller 内部调用它的 createdestroy 操作。实际上,按照我设置的方式,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 %>

如果我单击取消好友按钮,我会收到以下异常:

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

大家对此有什么想法吗?谢谢。

更新

友谊路线:

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 中传递的位置

最终结果如下:

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

关于ruby-on-rails-3 - Rails 从另一个 Controller 中调用 destroy 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8088645/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com