Ruby-on-rails Rails 新建与创建
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2472393/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rails new vs create
提问by sent-hil
Why is there a need to define a new method in RESTful controller, follow it up with a create method?
为什么需要在 RESTful 控制器中定义一个新方法,并使用 create 方法跟进?
Google search didn't provide me the answer I was looking for. I understand the difference, but need to know why they are used the way they are.
谷歌搜索没有为我提供我正在寻找的答案。我理解其中的区别,但需要知道为什么它们会被这样使用。
回答by Steve Weet
Within Rails' implementation of REST newand createare treated differently.
在 Rails 的 REST 实现中,new和create被区别对待。
An HTTP GET to /resources/newis intended to render a form suitable for creating a new resource, which it does by calling the newaction within the controller, which creates a new unsaved record and renders the form.
HTTP GET to/resources/new旨在呈现适合创建新资源的表单,它通过调用控制器内的新操作来实现,该操作创建新的未保存记录并呈现表单。
An HTTP POST to /resourcestakes the record created as part of the newaction and passes it to the createaction within the controller, which then attempts to save it to the database.
HTTP POST/resources将创建的记录作为新操作的一部分,并将其传递给控制器内的创建操作,然后尝试将其保存到数据库中。
回答by Justin Ethier
From the ActiveRecord::Basedocumentation:
create(attributes = nil) {|object| ...}
创建(属性 = 零){|对象| ...}
Creates an object (or multiple objects) and saves it to the database, if validations pass. The resulting object is returned whether the object was saved successfully to the database or not.
如果验证通过,则创建一个对象(或多个对象)并将其保存到数据库中。无论对象是否成功保存到数据库,都会返回结果对象。
new(attributes = nil) {|self if block_given?| ...}
new(attributes = nil) {|self if block_given?| ...}
New objects can be instantiated as either empty (pass no construction parameter) or pre-set with attributes but not yet saved (pass a hash with key names matching the associated table column names). In both instances, valid attribute keys are determined by the column names of the associated table — hence you can‘t have attributes that aren‘t part of the table columns.
新对象可以实例化为空(不传递构造参数)或预先设置属性但尚未保存(传递具有与关联表列名称匹配的键名称的散列)。在这两种情况下,有效的属性键由关联表的列名决定——因此您不能拥有不属于表列的属性。
So createinstantiates the new object, validates it, and then saves it to the database. And newonly creates the local object but does not attempt to validate or save it to the DB.
所以create实例化新对象,验证它,然后将它保存到数据库中。并且new只创建本地对象,但不尝试验证或将其保存到数据库。
回答by ghoppe
New instantiates a new Model instance, but it is not saved until the save method is called.
New 实例化一个新的 Model 实例,但在调用 save 方法之前它不会被保存。
Create does the same as new, but also saves it to the database.
Create 与 new 执行相同的操作,但还将其保存到数据库中。
Sometimes you want to do stuff before saving something to the database, sometimes you just want to create and save it straight away.
有时您想在将某些内容保存到数据库之前先做一些事情,有时您只想立即创建并保存它。
回答by Jared
The RESTful parts of Rails are made to be very close to how the HTTP protocol works. In the HTTP protocol, a GET request isn't supposed to modify any data. Logically, if you look at the way all of the RESTful actions in Rails work, they will match up with HTTP actions. A POST is for generating new data, so it is logically create. You use a GET to serve the form version of that or in other words, the new action. Index and show are also GETs, update is a PUT (or PATCH in Rails 4+), and destroy is a DELETE in HTTP.
Rails 的 RESTful 部分非常接近 HTTP 协议的工作方式。在 HTTP 协议中,GET 请求不应修改任何数据。从逻辑上讲,如果您查看 Rails 中所有 RESTful 操作的工作方式,它们将与 HTTP 操作相匹配。POST 用于生成新数据,因此它在逻辑上是创建的。您使用 GET 来提供该表单版本,或者换句话说,新操作。Index 和 show 也是 GET,update 是 PUT(或 Rails 4+ 中的 PATCH),而 destroy 是 HTTP 中的 DELETE。
In addition, it nicely separates the logic in the controller and gives you a smooth way to deal with errors (by re-rendering the new action with error messages).
此外,它很好地分离了控制器中的逻辑,并为您提供了一种处理错误的流畅方式(通过重新渲染带有错误消息的新操作)。

