简体   繁体   English

Rails中脚手架的用例有哪些? 我应该何时使用而不使用它?

[英]What are the use cases of scaffolding in Rails? When should I use and not use it?

I've been doing Agile Development with Rails 4 which uses scaffolding in most parts of the book. 我一直在使用Rails 4进行敏捷开发,在本书的大部分内容中都使用了脚手架。 However, I've read online that scaffolding in Rails is actually quite bad but I don't really understand why. 但是,我在网上看到Rails中的脚手架实际上非常糟糕,但我真的不明白为什么。 Could anyone explain this to me? 谁有人向我解释这个? Thanks. 谢谢。

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. Michael Hartl的Rails Tutorial中,它被用作一种简单的方式来展示如何使Rails模型工作:需要什么代码以及在哪里。 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. 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. 完成后,您可能会生成一个新项目并从头开始实施更精美的版本。

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 . 使用Rails,您还可以构建永远不会在公共场所展示但将在内部使用的应用程序 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. 我亲自将它用于游戏,我将某些游戏内资源搭建为模型并调整输出JSON索引的方式。 Resources are filled in by everybody through convenient web interface, then exported to JSON file and put into into the game assets. 每个人都通过方便的Web界面填充资源,然后导出到JSON文件并放入游戏资产。 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). 这节省了时间 ,我只浪费了15-20分钟,并节省了我们维护数据集的额外工作时间(结构总是保持正确,数据可以由几个人并行填充)。

I tried it in a previous version of rails. 我在之前版本的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. 它基本上是一种从数据库到html视图以及介于两者之间的所有内容创建应用程序的基本完整堆栈实现的方法。 It really only seems useful if you are building a very basic application that only supports crud. 如果你正在构建一个只支持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比在实际项目中实际使用它作为设计方法更有用。

It's an easy way to get an application off the ground if you just need something to experiment with. 如果您只是需要尝试一些东西,这是一个简单的方法来启动应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM