0

一直试图解决这个问题,但无法弄清楚我做错了什么。我只是想破坏两个用户之间的友谊。

视图如下所示:

友谊/index.html.erb

<% @users.each do |user| %>

<% if user.id != current_user.id %> 
<%=h user.name %> 
<%if current_user.is_friend? user %>
<%= link_to "Destroy", friendships_path(:friend_id => user), :method => :delete %> 

<%else%>
<%= link_to "Add friend", friendships_path(:friend_id => user), :method => :post %> 
<% end %>
 <% end %>

<% end %>

友谊控制器

    def index
      @users = User.all
    end

    def show
     @user = current_user
    end

 def create
   @friendship = current_user.friendships.build(:friend_id => params[:friend_id])
   if @friendship.save
     flash[:notice] = "Added friend."
     redirect_to root_url
   else
     flash[:error] = "Error occurred when adding friend."
     redirect_to root_url
   end
 end

def destroy
  @friendship = current_user.friendships.find(params[:id])
  @friendship.destroy
  flash[:notice] = "Successfully destroyed friendship."
  redirect_to root_url
end  

我收到此错误:

没有路线匹配 [DELETE] "/friendships"

但是,当我运行 rake 路线时,我似乎有路径:

     friendship GET    /friendships/:id(.:format)         friendships#show
                PUT    /friendships/:id(.:format)         friendships#update
                DELETE /friendships/:id(.:format)         friendships#destroy
4

1 回答 1

3

这条线

<%= link_to "Destroy", friendships_path(:friend_id => user), :method => :delete %>

应该看起来像

<%= link_to "Destroy", friendship_path(friendship), :method => :delete %>

(单数,不是复数形式)

是 Friendship 对象的friendship一个​​实例,获取方式取决于模型代码。最有可能的是,像

<%= link_to "Destroy", friendship_path(current_user.friendships.find_by_friend_id(user)), :method => :delete %>

应该管用。

于 2012-12-09T17:55:53.427 回答