简体   繁体   English

REST API的用户资源

[英]User Resources for REST API

I am using the devise gem for rails . 我正在使用devise gem作为rails

The default routes generated for users include some of the following routes: 为用户生成的默认路由包括以下一些路由:

    new_user_password GET    /users/password/new(.:format)                                                            devise/passwords#new
   edit_user_password GET    /users/password/edit(.:format)                                                           devise/passwords#edit
        user_password PATCH  /users/password(.:format)                                                                devise/passwords#update
                      PUT    /users/password(.:format)                                                                devise/passwords#update
                      POST   /users/password(.:format)                                                                devise/passwords#create

I'm trying to understand why they didn't use /users/:id/password , etc.? 我试图了解为什么他们不使用/users/:id/password等?

For example, I'm adding a user preference route. 例如,我要添加一个用户preference路由。 I can follow the same logic and make something like /users/preferences but it feels like it should be /users/:id/preferences based on the currently logged in users. 我可以遵循相同的逻辑,进行类似/users/preferences但是根据当前登录的用户,它应该是/users/:id/preferences However, I'm trying to keep everything similar and devise already set the precedent unless I change it. 但是,除非我更改它,否则我将尽力保持所有相似之处并devise先例。

You could do /users/:id/preferences . 可以执行/users/:id/preferences But, params[:user_id] will (presumably) equal current_user.id . 但是, params[:user_id] (大概)将等于current_user.id So, it's a bit redundant. 因此,这有点多余。

Instead, you can just do /users/preferences and access the relevant id from current_user . 相反,您可以执行/users/preferences并从current_user访问相关的id (Which, if you like this sort of thing, obscures the current user's id by not including it in the url.) (如果您喜欢这种方式,则可以通过不将其包含在网址中来掩盖当前用户的ID。)

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

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