Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails RESTAPI的用户资源_Ruby On Rails_Rest_Api_Devise - Fatal编程技术网

Ruby on rails RESTAPI的用户资源

Ruby on rails RESTAPI的用户资源,ruby-on-rails,rest,api,devise,Ruby On Rails,Rest,Api,Devise,我正在为rails使用designegem 为用户生成的默认路由包括以下一些路由: new_user_password GET /users/password/new(.:format) devise/passwords#new edit_user_password GET /users/password/edit(.:format)

我正在为
rails
使用
designe
gem

为用户生成的默认路由包括以下一些路由:

    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
我试图理解他们为什么不使用
/users/:id/password

例如,我正在添加一个用户
首选项
路由。我可以遵循相同的逻辑,制作类似于
/users/preferences
的内容,但根据当前登录的用户,我觉得应该是
/users/:id/preferences
。然而,我正试图保持一切相似,并且
设计
已经开创了先例,除非我改变它。

你可以做
/users/:id/preferences
。但是,
params[:user\u id]
将(大概)等于
current\u user.id
。所以,这有点多余

相反,您只需执行
/users/preferences
并从
当前用户
访问相关的
id
。(如果您喜欢这类内容,可以通过不将当前用户的id包含在url中来隐藏它。)

您可以执行
/users/:id/preferences
。但是,
params[:user\u id]
将(大概)等于
current\u user.id
。所以,这有点多余


相反,您只需执行
/users/preferences
并从
当前用户
访问相关的
id
。(如果您喜欢这类内容,可以通过不将当前用户的id包含在url中来隐藏它。)

因此基本上,经验法则是,如果您不需要将页面公开给外部世界,并且用户访问路由的唯一方法是当前登录,那么就不需要使用id?就REST标准而言,这是一个好的设计吗?考虑到您所描述的上下文,您的经验法则似乎是合理的。至于REST标准,我不是专家。但是,在我看来,您仍然在使用标准HTTP谓词来访问
首选项
资源,所以我认为这是可以的。为了避免最终的混乱,你使用
/current\u user/
而不是
/users/
,所以基本上经验法则是,如果你不需要向外界公开页面,并且用户访问路由的唯一方式是当前登录,那么就不需要使用id?就REST标准而言,这是一个好的设计吗?考虑到您所描述的上下文,您的经验法则似乎是合理的。至于REST标准,我不是专家。但是,在我看来,您仍然在使用标准的HTTP谓词访问
首选项
资源,因此我认为这是可以的。为了避免最终的混淆,您使用
/current\u user/
而不是
/users/