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. 《Java编程思想》笔记 第六章 访问权限控制

    1.编译单元 一个 编译单元即 .java 文件 内只能有一个 public 类  且该文件名必须与public 类名 完全一致. 编译单元内也可以没有public类 文件名可随意. 2. 包:库单元 ...

  2. KVM的qemu-kvm使用

    KVM: kvm,x86支持硬件辅助虚拟化技术(hvm) grep -E "(vmx|svm)" /proc/cpuinfo [root@dmsag ~]# ll /dev/kvm ...

  3. eclipse怎么关闭spring dashboard

    进入help-install new software-what is already installed?-卸载spring board

  4. Django remedy a security issue refer dos attack

    Today the Django team is issuing multiple releases -- Django 1.4.8, Django 1.5.4, and Django 1.6 bet ...

  5. EasyUI中combobox的代码实例

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. mysql table status

    SHOW TABLE STATUS 能获得表的信息 可以SHOW TABLE STATUS where name='表名'

  7. 回溯法练习【BFS/DFS】

    1.N皇后问题 2.油田问题 3.素数环问题 4.马踏棋盘问题 5.图的m着色问题 6.01背包问题 7.TSP问题 [Code-1:输出N皇后方案和个数] #include<bits/stdc ...

  8. uva11168

    uva11168 题意 给出一些点坐标,选定一条直线,所有点在直线一侧(或直线上),使得所有点到直线的距离平均值最小. 分析 显然直线一定会经过某两点(或一点),又要求点在直线某一侧,可以直接求出凸包 ...

  9. RPD Volume 168 Issue 4 March 2016 评论2

    Influence of the phantom shape (slab, cylinder or Alderson) on the performance of an Hp(3) eye dosem ...

  10. 洛谷 P4173 残缺的字符串

    (不知道xjb KMP可不可以做的说) (假设下标都以0开头) 对于有一定偏移量的序列的 对应位置 匹配或者数值计算的题,这里是有一种套路的,就是把其中一个序列翻转过来,然后卷积一下,所得到的新序列C ...