Rails 新建与创建 [英] Rails new vs create

查看:17
本文介绍了Rails 新建与创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么需要在 RESTful 控制器中定义一个新方法,并用一个 create 方法跟进?

Why is there a need to define a new method in RESTful controller, follow it up with a create method?

Google 搜索没有为我提供我正在寻找的答案.我理解其中的区别,但需要知道为什么它们会被这样使用.

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.

推荐答案

在 Rails 的 REST 实现中,newcreate 被区别对待.

Within Rails' implementation of REST new and create are treated differently.

/resources/new 的 HTTP GET 旨在呈现适合创建新资源的表单,它通过调用控制器中的 new 操作来实现,这会创建一个新的未保存记录并呈现表单.

An HTTP GET to /resources/new is intended to render a form suitable for creating a new resource, which it does by calling the new action within the controller, which creates a new unsaved record and renders the form.

/resources 的 HTTP POST 获取作为 new 操作的一部分创建的记录,并将其传递给控制器​​中的 create 操作,然后尝试将其保存到数据库中.

An HTTP POST to /resources takes the record created as part of the new action and passes it to the create action within the controller, which then attempts to save it to the database.

这篇关于Rails 新建与创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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