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. EasyUI的tree展开所有的节点或者根据特殊的条件控制展示指定的节点

    展示tree下的所有节点$(function(){ $('#t_funinfo_tree').tree({ checkbox: true, url:"<%=basePath %> ...

  2. jQuery插件--zTree中点击节点实现页面跳转时弹出两个页面的问题

    这是第一次使用zTree,所以在使用之前我要先写一个demo来学习一下.我们要注意的是,zTree是一个jQuery插件,所以我们在导入zTree的js文件之前要先导入jQuery的js文件. 我们先 ...

  3. KVM(三)I/O 全虚拟化和准虚拟化

    在 QEMU/KVM 中,客户机可以使用的设备大致可分为三类: 1. 模拟设备:完全由 QEMU 纯软件模拟的设备. 2. Virtio 设备:实现 VIRTIO API 的半虚拟化设备. 3. PC ...

  4. ssm框架整合配置文件

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  5. Linux下查看nginx、mysql、php的安装路径和编译参数

    一:查看安装路径: 1.nginx安装路径: ps  -ef | grep nginx 摁回车,将出现如下图片: master process 后面的就是 nginx的目录. 2.mysql安装路径: ...

  6. AppScan8.7的两个细节亮点

    1.增加了对红极一时的Struts2的远程代码执行漏洞的检测 2.增加了对篡改价格这类应用逻辑缺陷的检测

  7. LPD Office插件使用指南

    LPD Office插件已经发布至Azure上,您可以在本机Outlook和Office Online使用该插件 一:在Outlook中使用 LPD Office插件 打开Outlook应用,并点击“ ...

  8. box-shadow用法简介

    语法: <strong>box-shadow:</strong><em><length></em><em><length& ...

  9. 洛谷 P3371 【模板】单源最短路径 【链式前向星+SPFA】

    题目描述 如题,给出一个有向图,请输出从某一点出发到所有点的最短路径长度. 输入输出格式 输入格式: 第一行包含三个整数N.M.S,分别表示点的个数.有向边的个数.出发点的编号. 接下来M行每行包含三 ...

  10. Lock wait timeout exceeded数据库死锁问题

    环境 MySQL5.5 现象 A.数据更新或新增后数据经常自动回滚. B.表操作总报 Lock wait timeout exceeded 并长时间无反应 解决方法 A.应急方法:show proce ...