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. k8s的deployment应用

    Kubernetes 通过各种 Controller 来管理 Pod 的生命周期.为了满足不同业务场景,Kubernetes 开发了 Deployment.ReplicaSet.DaemonSet.S ...

  2. python tips:列表推导

    看一个代码: a=[1,2,3,4,5,6,7,8,9] b=[5 if (i >3) else 1 for i in a] print(b) 这就是列表推导. 列表推导一般用在通过一个list ...

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

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

  4. (十六)MySQL集群galera实现

    (1)环境介绍 galera官网:http://galeracluster.com/downloads/ # cat /etc/redhat-release CentOS Linux release ...

  5. DFS之奇偶剪枝

    问题描述: 给定一个 N * M的迷宫+起点+终点 ,迷宫中有一些障碍无法穿过,问能否不重复也不停留地在刚好一共走 t 步出迷宫. 先上结论: 在理想情况下,s到e需要的最小步数为m=|ex-sx|+ ...

  6. 【bzoj2763】[JLOI2011]飞行路线 (分层图最短路)(优先队列dij)

    [bzoj2763][JLOI2011]飞行路线 2014年3月25日1,7260 Description Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在n个城 ...

  7. linux历史命令

    "忘记历史的Linux用户注定要输入很多信息.” 这也让强有力的历史命令(包括Bash shell的历史变体)不仅在援引之前执行命令而不需重新输入它们时有用,在调用其它很少用到的命令时也有用 ...

  8. duboo服务使用thrift协议 + MQ

    写一篇博客来记录从 Python 转型到 Java 的学习成果.整体架构: rpc: dubbo + thrift idl: thrift registeration: zookeeper MQ: k ...

  9. Longest Absolute File Path -- LeetCode

    Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...

  10. 【二分答案】【Heap-Dijkstra】bzoj2709 [Violet 1]迷宫花园

    显然最短路长度随着v的变化是单调的,于是可以二分答案,据说spfa在网格图上表现较差. #include<cstdio> #include<cstring> #include& ...