我正在编写一个 API 来查找用户email_id
.
我在将电子邮件作为参数发送时遇到错误。
路线.rb:
Rails.application.routes.draw do
namespace :api do
resources :users, only: [:show, :index] , param: :email
end
end
当我将电子邮件作为参数发送时,出现错误。网址是http://localhost:3000/api/users/[电子邮件受保护] http://localhost:3000/api/users/test@abc.com:
ActiveRecord::RecordNotFound in Api::UsersController#show
Couldn't find User with 'id'={:email=>"test@abc"}
这是我的路线路径:
api_users_url GET /api/users(.:format) api/users#index
api_user_url GET /api/users/:email(.:format) api/users#show
您的电子邮件中有一个点.
, like @gmail.com
。这个点有问题。
默认情况下,Rails 路线中的动态段不接受点。
这就是为什么结果将是:
{
"sender"=>"emai@gmail",
"format"=>"com"
}
代替
{
"sender"=>"[email protected] /cdn-cgi/l/email-protection"
}
解决方案:
In your routes.rb
:
get 'message/:sender', to: 'main#message_sent', constraints: { sender: /[^\/]+/} , as: 'message_sent'
重要的部分是constraints: { sender: /[^\/]+/}
,它让您可以通过 url 参数传递一个点。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)