0

我正在 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"}

任何帮助将不胜感激。如果您需要更多信息,请告诉我。

谢谢。

4

2 回答 2

2

根据 rake 路由存在的路径是

user_friendship PUT    /users/:user_id/friendships/:id(.:format)         {:action=>"update", :controller=>"friendships"}

您正在使用的方法被放置,但您没有提供':id'。

根据您要执行的操作,有两种解决方案:

  1. 更改获取方法 - 这将指向新操作
  2. 将 :id 添加到您的 URL 构建器 - 这会将您指向更新操作

您的代码倾向于第二个,但我认为如果您想建立新的友谊,第一个会更好。

于 2011-01-31T14:20:01.050 回答
0

我对为什么 URL 助手映射到销毁操作感到困惑。但是有一个更简单的问题:user_friendship_path需要 params:user_id:id,而不是:friend_id

于 2011-01-31T04:04:56.533 回答