如何将CRUD和RESTFull保持在轨道中(具体示例) [英] How to keep it CRUD and RESTFull in rails (concrete example)

查看:115
本文介绍了如何将CRUD和RESTFull保持在轨道中(具体示例)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难让我的控制器不要太复杂,也很难保证我们在脚手架上时rails会执行的基本动作.

I've difficulties with keeping my controllers not too complicated and with the basic actions rails creates when we scaffold.

有人可以告诉我下面的方法是否正确.

Could someone tell me if the approach below is the right one.

我有一个模型Project及其基本路线:

I've a model Project and those basic routes it:

GET   /projects            ->   Project#index
GET   /projects/new        ->   Project#new
POST  /projects            ->   Project#create
GET   /projects/:id        ->   Project#show
GET   /projects/:id/edit   ->   Project#edit
PATCH /projects/:id        ->   Project#update
PUT   /projects/:id        ->   Project#update

现在,我想允许用户接受该项目.首先,他有一个摘要视图,可以在其中按一下按钮,它应该更改模型中的某些值.

Now I would like to allow the user to accept the project. First he has a summary view where he can push a button and it should change some value in the model.

我的第一次尝试是

GET   /projects/:id/accept ->   Project#accept
POST  /projects/:id/accept ->   Project#accept_update

这感觉不对.

因此,我想知道是否应该采用这种方式:

So I 'm wondering if I should do it in this way:

GET   /projects/:id/accept ->   Acceptations#new
POST  /projects/:id/accept ->   Acceptations#create

但是,然后,我有了我的接受"控制器来处理Project模型.什么开始让我感到困惑.

But then, I've my Acceptations controller that is dealing with Project models. What starts being confusing for me.

最重要的是,如果我选择第二个选项,路径应该是/projects/:id/acceptations/new,这对于最终用户来说似乎很长且令人困惑.

On top of that, if I go with the second options, should the path be /projects/:id/acceptations/new this seems long and confusing for the end user.

推荐答案

有一个约定(有时称为"heroku REST风格")将这些额外"方法作为操作"放置在主要资源下.

There is a convention (sometimes known as "heroku REST style") to put these "extra" methods as "actions" under the main resource.

因此,您可以使用它来接受项目.

So you'd have this for accepting the project.

 POST /projects/:id/actions/accept

从REST角度来看,此路由解析的内容无关紧要.可能是ProjectsController中的动作.我可能会为此操作创建一个专用控制器controllers/projects/actions/accept_controller.rb

What this route resolves to doesn't matter from the REST viewpoint. Could be an action in ProjectsController. I would likely create a dedicated controller just for this action, controllers/projects/actions/accept_controller.rb

对于new路由,json api不需要它.而且在使用GUI(例如您的网站)的情况下,您可能也不需要单独的页面.取而代之的是,它只是在项目显示页面上某处带有接受"按钮的小型表单.

As for the new route, you don't need it for json api. And in case of a GUI (such as your website), you maybe don't need a separate page as well. Instead, it'd just be a small form with "accept" button somewhere on project show page.

这篇关于如何将CRUD和RESTFull保持在轨道中(具体示例)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆