If you’re just learning Ruby on Rails, you may be confused as to when to generate individual models, resources or scaffolding, and what files are created by each command.

Say you want to generate a Test model with a name.  You could either generate the model individually, generate resources, or generate scaffolding, as follows:

rails g model Test name:text

rails g resource Test name:text

rails g scaffold Test name:text

What’s the difference between each of the above?

Entering rails g model Test name:text in your command line will generate the following:

(1) A model file test.rb in your  models directory:

class Test < ActiveRecord::Base
end

(2) A migration file timestamp_create_tests.rb in your db/migrate directory:

class CreateTests < ActiveRecord::Migration
def change
create_table :tests do |t|
t.text :name t.timestamps
end
end
end


Entering rails g resource Test name:text in your command line will generate the following:

(1) A model file test.rb in your  models directory:

class Test < ActiveRecord::Base
end

(2) A migration file timestamp_create_tests.rb  in your db/migrate directory:

class CreateTests < ActiveRecord::Migration
def change
create_table :tests do |t|
t.text :name t.timestamps
end
end
end

(3) a tests_controller.rb file in your  controllers directory.  This controller will be an empty shell:

class TestsController < ApplicationController
end

(4) resources :tests routes in your routes.rb file.



Entering rails g scaffold Test name:text in your command line will generate the following:

(1) A model file test.rb in your  models directory:

class Test < ActiveRecord::Base
end

(2) A migration file timestamp_create_tests.rb in your db/migrate directory:

class CreateTests < ActiveRecord::Migration
def change
create_table :tests do |t|
t.text :name t.timestamps
end
end
end

(3) A tests_controller.rb file in your  controllers directory.  When a scaffold is generated, seven public methods and two private methods will be added to your controller:

(4) resources :tests routes in your routes.rb file.

(5) Seven corresponding view files in your  views directory: (a) _form.html.erb, (b) edit.html.erb, (c) index.html.erb, (d) index.json.jbuilder, (e) new.html.erb, (f) show.html.erb and (g) show.json.jbuilder. Each view will contain html and embedded ruby.

rails generate model/resource/scaffold的区别的更多相关文章

  1. Rails generate的时候不生成assets和test

    我们在执行rails g controller controller_name或者rails g model model_name的时候往往会生成相应的assets文件和test,怎么不让rails帮 ...

  2. 【Ruby on Rails】Model中关于保存之前的原值和修改状态

    今天在Rails的Model中遇到了一个问题—— 当我从Model类中获取了一个ActiveRecord对象,对其进行了一系列修改(尚未保存),我该如何确定究竟哪些修改了呢? (设Model为Opti ...

  3. Spring中@Autowired注解、@Resource注解的区别 (zz)

    Spring中@Autowired注解.@Resource注解的区别 Spring不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定义的注解,它们分别是@Resource.@ ...

  4. @Autowired @Resource @Qualifier的区别

    参考博文: http://www.cnblogs.com/happyyang/articles/3553687.html http://blog.csdn.net/revent/article/det ...

  5. vue使用填坑之:model和v-model的区别

    v-model通常用于input的双向数据绑定 <input v-model="parentMsg">,也可以实现子组件到父组件数据的双向数据绑定:首先说说v-mode ...

  6. Rails :.nil? , .empty?, .blank? .present? 的区别

    .nil? , .empty?, .blank? .present? 的区别 首先这三个都是判空的. 而 .nil? 和 .empty? 是ruby的方法. .blank? 是rails的方法 .ni ...

  7. Spring中@Autowired注解、@Resource注解的区别

    Spring不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定义的注解,它们分别是@Resource.@PostConstruct以及@PreDestroy. @Resour ...

  8. 转:Spring中@Autowired注解、@Resource注解的区别

    Pay attention: When using these annotations, the object itself has to be created by Spring context. ...

  9. @Autowired标签与 @Resource标签 的区别

    Spring不但支持自己定义的@Autowired注解,还支持由JSR-250规范定义的几个注解,如:@Resource. @PostConstruct及@PreDestroy. 1. @Autowi ...

随机推荐

  1. mybatis generator 生成带中文注释的model类

    将org.mybatis.generator.interal.DefaultCommentGenerator类的addFieldComment方法重写,代码如下: public void addFie ...

  2. django使用celery进行耗时任务的优化

    # 原创,转载请留言联系 在用django做项目的时候,做到注册模块时,需要发送短信验证码.本来简简单单的做好了,后来优化的时候发现,发送短信验证码的时候需要一点时间,在这个时间之内程序是阻塞的,用户 ...

  3. selenium 3.0鼠标事件 (java代码)

    注意:ActionChains下相关方法在当前的firefox不工作,建议使用谷歌浏览器. public static void main(String[] args) throws Interrup ...

  4. 深入理解Java的注解(Annotation):基本概念(1)

    zz自:http://www.cnblogs.com/peida/archive/2013/04/23/3036035.html 什么是注解(Annotation): Annotation(注解)就是 ...

  5. PHP极速开发框架LotusAdmin page版发布

    体验地址及账号如下: 地址    https://page.waytomilky.com/ 账号:admin 密码:123456 LotusAdmin2.0其主要特性包括: 1.基于ThinkPHP5 ...

  6. hdu5790

    都快忘了在这类题的经典做法了…… 将字符串一个个的插入字典树,在字典树维护好有该前缀串s的最大编号字符串j,我们记作j控制了前缀串s 对于当前的第i个字符串,维护此时有当前每个字符串控制了多少个前缀串 ...

  7. nodejs 服务器重新启动

    在 我们开发node 应用的时候,一但你的应用已经启动了,这个时候如果你修改了服务端的文件,那么要是这个修改起作用,你必须手动停止服务然后再重新启动,这在开发过程中无 疑是很烦人的一件事,最好是有一个 ...

  8. 12、Django实战第12天:课程机构列表页数据展示

    今天完成的是课程机构列表页.... 1.启动服务,进入xadmin后,添加5个城市信息用作测试数据 2.添加课程机构,其中有一项要上传封面图的地方要注意 封面图上传路径是我们在models中设置好的 ...

  9. [BZOJ3206][APIO2013]道路费用(最小生成树)

    3206: [Apio2013]道路费用 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 568  Solved: 266[Submit][Status ...

  10. SD 一轮集训 day1 carcar

    可以发现每条边只能选一次或者两次,并且最后每个点的度数(∑邻接边选的次数和)都是偶数(代表有欧拉回路). 然后根据题意列一个 n 行 m+1 列的01矩阵,每一行代表一个异或方程组(每个点的度数是偶数 ...