简体   繁体   English

Ruby on Rails:脚手架、Model 和属性

[英]Ruby on Rails: Scaffolding, the Model, and attributes

I have an odd question.我有一个奇怪的问题。 Let's assume I create something using scaffolding.假设我使用脚手架创建了一些东西。 This should build me lots of different files including files in the model, view, and controller.这应该会为我构建许多不同的文件,包括 model、视图和 controller 中的文件。 I passed it not only the name of the scaffold, but also attributes/fields/variables.我不仅传递了脚手架的名称,还传递了属性/字段/变量。 When I looked at the model of the scaffold I generated, I don't see those attributes listed.当我查看我生成的脚手架的 model 时,我没有看到列出的那些属性。 I am puzzled.我很困惑。 How does Rails know that a field exists or not? Rails 如何知道一个字段是否存在? Can I change them in the future to not have a certain attribute or add a new attribute?我可以在将来将它们更改为不具有某个属性或添加新属性吗?

I apologize about this question.我为这个问题道歉。 I am feeling rather overwhelmed by trying to learn this framework.试图学习这个框架,我感到相当不知所措。 I'm originally a java developer working on small school projects and I want to branch out.我最初是一名从事小型学校项目的 java 开发人员,我想拓展业务。 I purchased and have been trying to read the Agile Web Development with Rails book that I keep hearing about.我购买并一直在尝试阅读我一直听说的 Agile Web Development with Rails 一书。 It's good, but I'm still feeling rather lost in how everything works.这很好,但我仍然对一切如何运作感到相当迷茫。 Am I just trying too hard to understand how rails works?我是不是太努力理解 Rails 的工作原理了?

Thank you all!谢谢你们!

Scaffolding is a way to generate code, which you will write otherwise.脚手架是一种生成代码的方法,否则您将编写代码。 Generated code is only a starting point and not the final version of things.生成的代码只是一个起点,而不是事物的最终版本。

Model: When you scaffold, you pass in fields which you need right now. Model:当你搭建脚手架时,你传入了你现在需要的字段。 Rails will generate the model and the migration for you. Rails 将为您生成 model 和迁移。 When you run the migration, it will create the table in db with the columns/fields you passed.当您运行迁移时,它将使用您传递的列/字段在 db 中创建表。 Model can dynamically determine what are the fields in the table. Model可以动态判断表中有哪些字段。

If you need to add/remove fields in future, you would need to write another migration to add remove them, which you can do using a rails generate migration如果将来需要添加/删除字段,则需要编写另一个迁移来添加删除它们,您可以使用rails generate migration

Controller: Scaffolding generates a standard controller with 7 actions which respond to the restful resource(your model): new, create, edit, update, destroy, show, index. Controller:脚手架生成一个标准的 controller,其中包含 7 个响应静态资源(您的模型)的操作:新建、创建、编辑、更新、销毁、显示、索引。 You can add or remove actions as you please.您可以随意添加或删除操作。 You would need to alter the routes accordingly if you add/remove the actions.如果您添加/删除操作,您将需要相应地更改路线。

Views: Rails scaffolding will generate the barebone standard views, with forms for your fields and views to show those fields.视图:Rails 脚手架将生成准系统标准视图,forms 用于您的字段和视图以显示这些字段。 You can customize the looks and change the views as per your requirements.您可以根据需要自定义外观并更改视图。

I hope it clarifies things.我希望它能澄清事情。

Scaffolding is not the only way to generate code.脚手架并不是生成代码的唯一方式。 There are generators specific to model, migrations and controllers.有特定于 model、迁移和控制器的生成器。

I hope you have read RailsGuides .我希望你已经阅读过 RailsGuides They are very helpful for beginners as well as people familiar with rails.它们对初学者和熟悉 Rails 的人都非常有帮助。

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

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