我正在 RoR 网站内建立友谊。它的模型是 user_id、friend_id 和 pending(布尔值)。我大部分时间都在关注 RailsCast 关于友谊的内容,但也对其进行了一些更改。到目前为止,我所拥有的是,当您转到用户页面时,您可以单击 Request Friendship 并且代码使用:
user_friendships_path(current_user, :friend_id => @user), :method => :post
这将调用 Friendships 控制器中的 create 方法。它会自动将pending设置为true。我现在想要的是有一个接受它的链接,它只会将待处理变为错误。所以我试图像这样设置它
(<%= link_to "Accept", user_friendship_path(:user_id => current_user.id, :friend_id => friendship.user.id, :pending => 'false'), :method => :put %>)
我实际上并不想去编辑页面,因为它只需要将该布尔值设置为false,所以我想直接调用更新。但是当我运行这个页面时,我得到了错误:
No route matches {:action=>"destroy", :controller=>"friendships", :user_id=>1, :friend_id=>2, :pending=>"false"}
我不明白为什么。我没有调用destroy(使用:method => :delete),实际上Friendship 控制器中有一个destroy 方法。
资源设置如下:
resources :users do
resources :friendships
end
运行“rake routes”的路径是:
user_friendships GET /users/:user_id/friendships(.:format) {:action=>"index", :controller=>"friendships"}
user_friendships POST /users/:user_id/friendships(.:format) {:action=>"create", :controller=>"friendships"}
new_user_friendship GET /users/:user_id/friendships/new(.:format) {:action=>"new", :controller=>"friendships"}
edit_user_friendship GET /users/:user_id/friendships/:id/edit(.:format) {:action=>"edit", :controller=>"friendships"}
user_friendship GET /users/:user_id/friendships/:id(.:format) {:action=>"show", :controller=>"friendships"}
user_friendship PUT /users/:user_id/friendships/:id(.:format) {:action=>"update", :controller=>"friendships"}
user_friendship DELETE /users/:user_id/friendships/:id(.:format) {:action=>"destroy", :controller=>"friendships"}
任何帮助将不胜感激。如果您需要更多信息,请告诉我。
谢谢。