ruby on Rails 没有路线与操作匹配:删除

发布于 2024-10-15 07:11:49 字数 1932 浏览 4 评论 0原文

我正在 RoR 网站内建立友谊。它的模型是user_id、friend_id 和pending(布尔值)。我大部分时间都在关注 RailsCast 的友谊,但也对其进行了一些更改。到目前为止,我所知道的是,当您转到用户页面时,您可以单击“请求友谊”,并且代码使用:

user_friendships_path(current_user, :friend_id => @user), :method => :post

这会调用“友谊”控制器中的创建方法。它会自动将挂起设置为 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 路线”的路径是:

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"}

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

谢谢。

I'm setting up friendships within a RoR website. The model for it is user_id, friend_id, and pending (boolean). I followed the RailsCast on friendships for the most part, but making some changes to it too. What I have so far is that when you go to a user's page you can click Request Friendship and the code uses:

user_friendships_path(current_user, :friend_id => @user), :method => :post

This calls the create method in the Friendships controller. It automatically sets pending to true. What I want now is to have a link to Accept it which would just turn pending to false. So I am trying to set it up like

(<%= link_to "Accept", user_friendship_path(:user_id => current_user.id, :friend_id => friendship.user.id, :pending => 'false'), :method => :put %>)

I don't actually want to go to the edit page, because it just needs to set that boolean to false, so I want to call the update directly. But when I run this page, I get the error:

No route matches {:action=>"destroy", :controller=>"friendships", :user_id=>1, :friend_id=>2, :pending=>"false"}

I don't understand why. I'm not calling the destroy (that would be with :method => :delete), and there actually is a destroy method within the Friendship controller.

The resources are set up like:

resources :users do
     resources :friendships
end

And the paths from running "rake routes" are:

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"}

Any help would be greatly appreciated. Please let me know if you require more information.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

亚希 2024-10-22 07:11:49

根据 rake 路由存在的路径是

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

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

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

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

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

The path that exists according to rake routes is

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

The method you are using is put, but you aren't supplying ':id'.

There are two solutions depending on what you are trying to do:

  1. Change the method to get - that will point you to the new action
  2. Add :id to your URL builder - that will point you to the update action

Your code is leaning towards the second, but I think if you want to create a new friendship, the first would be better.

窗影残 2024-10-22 07:11:49

我很困惑为什么 URL 帮助器映射到销毁操作。然而,有一个更简单的问题:user_friendship_path需要参数:user_id:id,而不是:friend_id

I'm baffled as to why the URL helper is mapping to a destroy action. However there is a simpler problem: user_friendship_path expects params :user_id and :id, not :friend_id.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文