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

查看:51
本文介绍了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项目中输入命令rails generate model Foo name:string description:text,我会得到:

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"的数据库表,还将为名称"和描述"创建列.

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,将改为创建spec文件.

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应用程序仅包含模型,则您将没有任何一种在页面上显示信息的视图,也没有控制信息流的指令.您的选择是还生成控制器(后者又生成您的视图)或生成一个脚手架,该支架生成您的模型,视图,控制器并写入route.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 generate 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天全站免登陆