Rails 中脚手架和模型的区别 [英] difference between scaffold and model in Rails

查看:18
本文介绍了Rails 中脚手架和模型的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Rails 中生成脚手架和生成模型有什么区别?这样做有什么优点/缺点?

What's the difference between generating a scaffold and generating a model in Rails? What are the advantages/disadvantages of doing either?

推荐答案

当你生成一个模型时,你会得到一个模型以及一些相关的组件.我最喜欢的解释此类主题的方法之一是实际尝试或鼓励其他人尝试,因此如果我要输入命令 rails generate model Foo name:string description:textRails 项目,我会得到:

When you generate a model, you get a model as well as some related components. One of my favorite ways of explaining topics like this is to actually try it out or encourage others to try it, so if I were to enter the command rails generate model Foo name:string description:text within a Rails project, I would get:

invoke  active_record
  create    db/migrate/20130719012107_create_foos.rb
  create    app/models/foo.rb
  invoke    test_unit
  create      test/unit/foo_test.rb
  create      test/fixtures/foos.yml

第一行调用 Active Record,它基本上将您的模型与数据库联系起来.下一行创建所谓的迁移文件.迁移文件包含更改数据库的说明.第一个迁移文件创建名为foos"的数据库表,它还将为name"和description"创建列.

The first line invokes Active Record, which basically ties your model to your database. The next line creates what's called a migration file. Migration files contain instructions for altering your database. This first migration file creates the database table called 'foos' and it will also create columns for "name" and "description".

下一行创建模型本身.该模型基本上是一个继承自 Active Record 的 Ruby 类.这意味着可以在 Active Record 中调用的任何方法现在都可以在您的模型中调用.最后三行基本上为您的模型创建了相关的测试文件.如果您使用的是 RSpec,则将创建规范文件.

The next line makes the model itself. The model is basically a Ruby class that inherits from Active Record. What this means is that any methods that can be called in Active Record can now be called in your model. The last three lines basically create related test files for your model. If you were using RSpec, spec files would be created instead.

如果您的 Rails 应用程序只包含模型,那么您将不会有任何在页面上显示信息的视图,也不会有控制信息流的指令.您的选择是也生成控制器(它反过来生成您的视图)或生成一个脚手架,它生成您的模型、视图、控制器并写入您的 routes.rb 文件.如果我运行 rails generate scaffold foo 我会得到:

If your Rails application only contained models, you would not have any kind of view that displays information on a page, nor would you have instructions that control the flow of information. Your choices would be to also generate controllers (which in turn generates your views) or to generate a scaffold, which generates your model, views, controller, and writes to your routes.rb file. If I ran rails generate scaffold foo I would get:

invoke  active_record
  create    db/migrate/20130719013307_create_foos.rb
  create    app/models/foo.rb
  invoke    test_unit
  create      test/unit/foo_test.rb
  create      test/fixtures/foos.yml
  invoke  resource_route
   route    resources :foos
  invoke  scaffold_controller
  create    app/controllers/foos_controller.rb
  invoke    erb
  create      app/views/foos
  create      app/views/foos/index.html.erb
  create      app/views/foos/edit.html.erb
  create      app/views/foos/show.html.erb
  create      app/views/foos/new.html.erb
  create      app/views/foos/_form.html.erb
  invoke    test_unit
  create      test/functional/foos_controller_test.rb
  invoke    helper
  create      app/helpers/foos_helper.rb
  invoke      test_unit
  create        test/unit/helpers/foos_helper_test.rb
  invoke  assets
  invoke    coffee
  create      app/assets/javascripts/foos.js.coffee
  invoke    scss
  create      app/assets/stylesheets/foos.css.scss
  invoke  scss
 identical    app/assets/stylesheets/scaffolds.css.scss

为了回答您的问题,脚手架的优势在于它快速、简单,并且一切都为您预先配置.但是,独立于脚手架生成模型(然后在需要的地方生成控制器/视图并自己编写 routes.rb 文件)的优点是您可以更好地控制您的应用程序及其外观和功能,您可以避免不必要的代码,您可以使用行为驱动开发或测试驱动开发,以及其他人可能想要添加的其他内容.

To answer your question, the advantage of the scaffold is that it's quick, easy, and everything is preconfigured for you. However, the advantages of generating models independently of scaffolds (and then in turn generating controllers/views where needed and writing your routes.rb file yourself) is that you have a lot more control over your app and how it looks and functions, you avoid unnecessary code, you can employ Behaviour-Driven Development or Test Driven Development, and probably other things that someone else might want to add.

我的最后一点建议是:Rails 非常用户友好,所以请尝试自己尝试.您可以使用相应的 destroy 命令撤消任何 generate 命令,例如 rails destroy scaffold Foo 将删除所有由 生成的文件rails 会生成 Scaffold Foo name:string description:string,这样你就不用担心通过实验不可避免地搞砸项目了.

My last bit of advice is: Rails is very user-friendly, so try experimenting yourself. You can undo any generate command with a corresponding destroy command, so for instance rails destroy scaffold Foo would delete all the files generated by rails generate Scaffold Foo name:string description:string, so you don't have to worry about irrevocably messing up a project by experimenting.

这篇关于Rails 中脚手架和模型的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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