我有一个,friendships_controller
但我想从. 实际上,按照我的设置方式,该方法可以正常工作,但不能。create
destroy
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 %>
如果我单击 Unfriend 按钮,我会得到跟随 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"}