Rails-map.resources的冗余RESTFUL动作? (新创建) [英] Rails - Redundant RESTFUL Actions for map.resources? (new, create)

查看:71
本文介绍了Rails-map.resources的冗余RESTFUL动作? (新创建)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么当您使用map.resources在rails中创建静态路由时,它会为新建,创建,编辑,更新生成动作? 声明仅一项 create update 操作是否有错? 并做这样的事情?

I was wondering why when you create restful routes in rails with map.resources it generates actions for new, create, edit, update? Is there anything wrong in declaring just one action for create and update and do something like this?

def create
  unless post?
     @user = User.new
  else
     redirect_to :action => 'index' if user.create(params[:user])
  end
end

所以我们可以有类似的东西

so we could have something like

:GET  users/create # to show the form (same as action - new)
:POST users/create # to create a new user

由于Restful是基于动词的,所以这不是最好的使用方法吗?

since Restful is based on verbs, wouldn't this be the best approach to use?

感谢您的关注

推荐答案

我认为这里有两个相关但截然不同的问题:公开的URL和路由到它们的控制器方法.由于这两个都可以独立更改,因此我将分别解决它们.另外,请注意,我将稍微松散地讲,严格讲讲在Rails上下文中实现的REST.

I think there are two related but distinct issues here: the URLs exposed and the controller methods they are routed to. Since either of these could be changed independently, I'll address them separately. Also, please note that I will be speaking a bit loosely, and strictly about REST as implemented in the context of Rails.

就外部URL而言,我认为这有助于区分组成系统API的URL(:GET users/1:PUT users/1等)和为人类提供方便的URL使用网络浏览器(users/newusers/5/edit等). API就是关于获取资源或以某种方式与它们进行交互的-这些是与系统进行交互时另一台计算机将要使用的URL.这些URL通常只是您要与之交互的资源的地址,然后使用HTTP方法和参数来指示您要执行的操作(GET =向我展示此资源,PUT =更改此资源,等等. ).便利URL在那里显示一种表格,使人们更容易使用API​​.您可以使用curl手动输入要更改的所有参数并向用户/1进行POST,从而可以 来编辑用户,但是作为人类,如果您仅使用表格就可以轻松得多

In terms of the external URLs, I think it helps to distinguish between the URLs that make up the API of the system (:GET users/1, :PUT users/1, etc) and the URLs that are just there as a convenience to humans using a web browser (users/new, users/5/edit, etc). The API is all about fetching resources or interacting with them in some way - these are the URLs that another computer is going to use when interacting with your system. These URLs are typically just the address of the resource you want to interact with, and then you use the HTTP method and the parameters to indicate what it is you want to do (GET = show me this resource, PUT = change this resource, etc). The convenience URLs are there to display a form to make it easier for a human to use the API. You could edit a user by using curl to manually type out all the parameters you wanted to change and make a POST to users/1, but as a human it's a lot easier if you can just use a form.

在上面的示例中,:GET users/create可能有意义(并且与:GET users/new非常相似,这是默认设置),但是:POST users/create粗略地翻译为使用户/创建一个新用户",这没有任何意义.

To look at your examples above, then, :GET users/create might make sense (and is pretty similar to :GET users/new which is the default), but :POST users/create would roughly translate to "make a new one of users/create", which doesn't quite make sense.

就控制器方法而言,新"和创建"执行的是根本不同的任务,希望从前几段中可以清楚地看出来.其中一个正在显示表单,另一个正在创建新资源.当然,您可以 重载相同的方法来执行此操作,但没有令人信服的理由,创建两个小的独立方法来处理两个小的独立任务可能是更自然的方法.

As far as the controller methods go, "new" and "create" are performing fundamentally different tasks, as should hopefully be clear from the previous paragraphs. One of them is displaying a form, and the other is creating a new resource. You could overload the same method to do this, of course, but without a compelling reason to do so, creating two small independent methods to handle two small independent tasks is probably a more natural approach.

这篇关于Rails-map.resources的冗余RESTFUL动作? (新创建)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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