Rails中脚手架的用例是什么?我应该什么时候使用,什么时候不使用?

时间:2022-02-04 00:13:13

I've been doing Agile Development with Rails 4 which uses scaffolding in most parts of the book. However, I've read online that scaffolding in Rails is actually quite bad but I don't really understand why. Could anyone explain this to me? Thanks.

我一直在用Rails 4进行敏捷开发,在本书的大部分部分都使用了脚手架。然而,我在网上读到过Rails的脚手架实际上很糟糕,但我真的不明白为什么。有人能给我解释一下吗?谢谢。

2 个解决方案

#1


2  

The name itself suggests using it for scaffolding. The use cases of it vary.

这个名字本身就建议把它用作脚手架。它的用例是不同的。

In Rails Tutorial by Michael Hartl it is used as a simple way of showing how to make Rails models work: what code is needed and where. Scaffolding demonstrates some of the basic elements just about any Rails developer uses in his apps, so it's at least a good starting point for beginners.

在Michael Hartl的Rails教程中,它被用作展示如何使Rails模型工作的一种简单方式:需要什么代码以及在哪里。Scaffolding展示了任何Rails开发人员在其应用程序中使用的一些基本元素,因此它至少是初学者的一个良好起点。

Once you are not a beginner, and you can write code comparable to scaffolding, you don't use it too often because it's just not suitable: you'll end up digging through the generated code and fixing it to suit your needs for longer than you would just write what you need from scratch. Otherwise you might accidentally expose private data, allow some restricted actions for everyone and who knows what else. It's easier to keep control of your code when it's filled in gradually. When you scaffold a model, a lot of uncontrolled code appears.

一旦你不是一个初学者,你可以编写代码与脚手架,你不经常使用它,因为它是不合适的:最终你会挖掘生成的代码和修复它来适应你的需求超过你就写你需要从头开始。否则,您可能会意外地暴露私有数据,允许对每个人以及谁都知道的其他人进行一些受限的操作。当逐步填充代码时,更容易控制代码。当您构建模型时,会出现许多不受控制的代码。

However, if you are not building an app yet, but more like researching how to build it (like testing out various DB schemas), scaffolding will speed up the process by bringing in an environment to construct and view data in your database in a convenient way so you can test whether your ideas will play well for your needs. Once you are done, you're likely to generate a new project and implement a more polished version from scratch.

然而,如果你还没有建立一个应用程序,但更喜欢研究如何构建它(如测试各种DB模式)、脚手架将加速的过程带来的环境建设和数据库中的视图数据在一个方便的方式你可以测试你的想法是否将对您的需求。完成之后,您可能会生成一个新项目,并从头实现一个更完善的版本。

There are also exotic uses you might find funny. With Rails you can also build apps that will never be shown in public but will rather be used internally. One of such cases is construction of a dataset for something. I have personally used it for a game, I scaffolded a certain in-game resource as a model and tweaked the way it outputs JSON index. Resources are filled in by everybody through convenient web interface, then exported to JSON file and put into into the game assets. This saves time, I only wasted like 15-20 minutes and saved us from hours of extra work on maintaining the dataset (structure always remains correct, data can be filled by several people in parallel).

还有一些你可能觉得有趣的奇异用法。使用Rails,您还可以构建永远不会在公共场合显示的应用程序,而是在内部使用。其中一个例子是为某物构建数据集。我个人曾在一个游戏中使用它,我构建了一个游戏中的资源作为模型,并调整了它输出JSON索引的方式。每个人都可以通过方便的web界面填充资源,然后导出到JSON文件并放入到游戏资产中。这节省了时间,我只浪费了15-20分钟,也节省了我们在维护数据集上花费的时间(结构始终是正确的,数据可以由几个人并行填充)。

#2


1  

I tried it in a previous version of rails. It basically is a way to create a basic full stack implementation of your app from database to html views and everything in between. It really only seems useful if you are building a very basic application that only supports crud. I find that it's more useful for learning about rails than to actually use it as a design approach in a real world project.

我在rails的前一个版本中尝试过。它基本上是一种创建应用程序的基本完整栈实现的方法,从数据库到html视图,以及两者之间的所有东西。只有当您正在构建一个只支持crud的非常基本的应用程序时,它才显得真正有用。我发现学习rails比在真实的项目中把它作为一种设计方法更有用。

It's an easy way to get an application off the ground if you just need something to experiment with.

如果你只需要做一些实验,这是一种让应用程序启动的简单方法。

#1


2  

The name itself suggests using it for scaffolding. The use cases of it vary.

这个名字本身就建议把它用作脚手架。它的用例是不同的。

In Rails Tutorial by Michael Hartl it is used as a simple way of showing how to make Rails models work: what code is needed and where. Scaffolding demonstrates some of the basic elements just about any Rails developer uses in his apps, so it's at least a good starting point for beginners.

在Michael Hartl的Rails教程中,它被用作展示如何使Rails模型工作的一种简单方式:需要什么代码以及在哪里。Scaffolding展示了任何Rails开发人员在其应用程序中使用的一些基本元素,因此它至少是初学者的一个良好起点。

Once you are not a beginner, and you can write code comparable to scaffolding, you don't use it too often because it's just not suitable: you'll end up digging through the generated code and fixing it to suit your needs for longer than you would just write what you need from scratch. Otherwise you might accidentally expose private data, allow some restricted actions for everyone and who knows what else. It's easier to keep control of your code when it's filled in gradually. When you scaffold a model, a lot of uncontrolled code appears.

一旦你不是一个初学者,你可以编写代码与脚手架,你不经常使用它,因为它是不合适的:最终你会挖掘生成的代码和修复它来适应你的需求超过你就写你需要从头开始。否则,您可能会意外地暴露私有数据,允许对每个人以及谁都知道的其他人进行一些受限的操作。当逐步填充代码时,更容易控制代码。当您构建模型时,会出现许多不受控制的代码。

However, if you are not building an app yet, but more like researching how to build it (like testing out various DB schemas), scaffolding will speed up the process by bringing in an environment to construct and view data in your database in a convenient way so you can test whether your ideas will play well for your needs. Once you are done, you're likely to generate a new project and implement a more polished version from scratch.

然而,如果你还没有建立一个应用程序,但更喜欢研究如何构建它(如测试各种DB模式)、脚手架将加速的过程带来的环境建设和数据库中的视图数据在一个方便的方式你可以测试你的想法是否将对您的需求。完成之后,您可能会生成一个新项目,并从头实现一个更完善的版本。

There are also exotic uses you might find funny. With Rails you can also build apps that will never be shown in public but will rather be used internally. One of such cases is construction of a dataset for something. I have personally used it for a game, I scaffolded a certain in-game resource as a model and tweaked the way it outputs JSON index. Resources are filled in by everybody through convenient web interface, then exported to JSON file and put into into the game assets. This saves time, I only wasted like 15-20 minutes and saved us from hours of extra work on maintaining the dataset (structure always remains correct, data can be filled by several people in parallel).

还有一些你可能觉得有趣的奇异用法。使用Rails,您还可以构建永远不会在公共场合显示的应用程序,而是在内部使用。其中一个例子是为某物构建数据集。我个人曾在一个游戏中使用它,我构建了一个游戏中的资源作为模型,并调整了它输出JSON索引的方式。每个人都可以通过方便的web界面填充资源,然后导出到JSON文件并放入到游戏资产中。这节省了时间,我只浪费了15-20分钟,也节省了我们在维护数据集上花费的时间(结构始终是正确的,数据可以由几个人并行填充)。

#2


1  

I tried it in a previous version of rails. It basically is a way to create a basic full stack implementation of your app from database to html views and everything in between. It really only seems useful if you are building a very basic application that only supports crud. I find that it's more useful for learning about rails than to actually use it as a design approach in a real world project.

我在rails的前一个版本中尝试过。它基本上是一种创建应用程序的基本完整栈实现的方法,从数据库到html视图,以及两者之间的所有东西。只有当您正在构建一个只支持crud的非常基本的应用程序时,它才显得真正有用。我发现学习rails比在真实的项目中把它作为一种设计方法更有用。

It's an easy way to get an application off the ground if you just need something to experiment with.

如果你只需要做一些实验,这是一种让应用程序启动的简单方法。