简体   繁体   English

Rails 4:URL中的点以分隔ID

[英]Rails 4: Dot in URL to separate IDs

I use the following line to create a link 我使用以下行创建一个链接

link_to_remote 'Delete', :url => mail_path(user_id, mail.id), :confirm => 'Delete?', :method => :delete

This will create a DELETE request in the form of /mail/4.3 The route is 这将以/mail/4.3的形式创建DELETE请求。路由是

delete 'mail/:user_id/:id', to: 'mail#destroy'

I'm trying to access the two IDs in my controller with 我正在尝试使用控制器访问控制器中的两个ID

@mail = Mail.find(params[:id])
@user = User.find(params[:user_id])

However, it fails with error 但是,它失败并出错

ActionController::RoutingError (No route matches [DELETE] "/mail/4.3")

What am I missing here? 我在这里错过了什么?

Update 1) Output of "rake routes" 更新1)“rake路线”的输出

Prefix     Verb      URI Pattern                          Controller#Action
mail       DELETE    /mail/:user_id/:id(.:format)        mail#destroy

The reason it is producing the url /mail/4.3 is because mail_path only takes one parameter, so it takes the second as a format. 它产生url /mail/4.3的原因是因为mail_path只接受一个参数,所以它将第二个作为格式。 This is because the mail_path url helper that you're using isn't for the DELETE route that you've posted in your question, but some other route. 这是因为您使用的mail_path url帮助程序不适用于您在问题中发布的DELETE路由,而是一些其他路由。

The Rails Way would be to have nested resources for mail and user like: Rails方式将为mailuser提供嵌套资源,例如:

resources :users do
  resources :mail
end

Giving you the route helper user_mail_path(@user, @mail) (or something similar depending on pluralisation of mail) and the URL /users/4/mail/3 . 给你路由助手user_mail_path(@user, @mail) (或类似的东西,取决于邮件的多元化)和URL /users/4/mail/3

Alternatively, to continue on your path you'll probably have to name your delete route: 或者,要继续您的路径,您可能必须命名您的删除路线:

delete 'mail/:user_id/:id', to: 'mail#destroy', as: 'delete_mail'

and use delete_mail_path(user_id, mail.id) in your link. 并在链接中使用delete_mail_path(user_id, mail.id)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM