REST API应用 - 嵌套资源功能实现
我是在Ruby上在Rauds上写的第一个Restful API应用程序,这是学位课程的一部分。 最终项目即将为每个人提供一个简单的任务管理器,并为每个任务提供一系列任务。我以前从未使用过Ruby语言,因此尝试通过互联网使用一些手册。我成功地实现了人数据库,但是我认为我会遵守任务控制器中任务功能的一部分,因为它是嵌套的recuorce-每个用户都有自己的任务列表,每个任务对他来说都是唯一的。 我不确定如何实现下一个REST API命令:
get/perion/{id}/tasks/ - 返回具有ID ID的人拥有的一系列任务。
post/people/{id}/tasks/ - 添加了请求主体所描述的新任务,向ID等于ID的人。如果请求主体中未指定状态字段,则服务器将默认将新创建的任务标记为活动。
我已经将人和任务的活动记录之间的关联定义为has_many和allys_to,只是不确定如何实现该2个命令,我也必须实现修改和删除,但首先想了解如何实现上面提到的2个命令。 谢谢
I'm writing my first RESTful API app on ruby on rails as a part of a degree's course.
Final project is about to make a simple task manager with a base of users and an array of task for each one of them. I never worked with Ruby language before so tried to use some manuals over the internet. I succeed to implement people database, but I think I'd stuck with a part of a functions of tasks in Task controller, because it's nested resuorce-every user has own task list each task is unique for him.
I'm not really sure how to implement the next REST API commands:
GET /people/{id}/tasks/ - Returns an array of tasks that the person with id id owns.
POST /people/{id}/tasks/ - Adds the new task, as described by the request body, to the person whose id equals id. If the status field is not specified in the request body, the server will default to marking the newly created task as active.
I already defined the association between active records of person and tasks as has_many and belongs_to , just not sure how to implement that 2 commands,also i have to implement modify and delete, but first of all want to understand how to implement 2 commands mentioned above.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这里,您有2个解决方案,让我们一起通过它们。
第一,如果您要实现它以遵循此路线
get/people/{id}/tasks
在
peose_controller.rb
中,您可以执行以下操作,我不做推荐这种方式,因为这不是我们应该这样做的方式,您应该在
tasks_controller.rb
执行以下途径以创建任务将是
发布'/people/tasks',to:'your_controller_path#创建'
以及为特定用户获得任务的路线
获取'/people/tasks',to:'your_controller_path#show'
,不要忘记发送所需的参数Here you have 2 solutions, let's go through them together.
1st one, if you want implement it to follow this route
GET /people/{id}/tasks
in your
people_controller.rb
you can do the followingand I don’t recommend this way because it's not how we supposed to do it, you should do tasks crud in tasks controller
2ed way, in your
tasks_controller.rb
do the followingThe route to create a task will be
post '/people/tasks', to: 'your_controller_path#create'
and the route to get the tasks for a specfic user
get '/people/tasks', to: 'your_controller_path#show'
and don't forget to send the required params