rails generate model/resource/scaffold的区别
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的区别的更多相关文章
- Rails generate的时候不生成assets和test
我们在执行rails g controller controller_name或者rails g model model_name的时候往往会生成相应的assets文件和test,怎么不让rails帮 ...
- 【Ruby on Rails】Model中关于保存之前的原值和修改状态
今天在Rails的Model中遇到了一个问题—— 当我从Model类中获取了一个ActiveRecord对象,对其进行了一系列修改(尚未保存),我该如何确定究竟哪些修改了呢? (设Model为Opti ...
- Spring中@Autowired注解、@Resource注解的区别 (zz)
Spring中@Autowired注解.@Resource注解的区别 Spring不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定义的注解,它们分别是@Resource.@ ...
- @Autowired @Resource @Qualifier的区别
参考博文: http://www.cnblogs.com/happyyang/articles/3553687.html http://blog.csdn.net/revent/article/det ...
- vue使用填坑之:model和v-model的区别
v-model通常用于input的双向数据绑定 <input v-model="parentMsg">,也可以实现子组件到父组件数据的双向数据绑定:首先说说v-mode ...
- Rails :.nil? , .empty?, .blank? .present? 的区别
.nil? , .empty?, .blank? .present? 的区别 首先这三个都是判空的. 而 .nil? 和 .empty? 是ruby的方法. .blank? 是rails的方法 .ni ...
- Spring中@Autowired注解、@Resource注解的区别
Spring不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定义的注解,它们分别是@Resource.@PostConstruct以及@PreDestroy. @Resour ...
- 转:Spring中@Autowired注解、@Resource注解的区别
Pay attention: When using these annotations, the object itself has to be created by Spring context. ...
- @Autowired标签与 @Resource标签 的区别
Spring不但支持自己定义的@Autowired注解,还支持由JSR-250规范定义的几个注解,如:@Resource. @PostConstruct及@PreDestroy. 1. @Autowi ...
随机推荐
- shell里面比较大小
#!/bin/bashif [ $1 -gt $2 ]then echo "$1>$2"else echo "$2>$1"fi# 数字判断一些命令 ...
- Selenium2+python自动化23-富文本(自动发帖)【转载】
前言 富文本编辑框是做web自动化最常见的场景,有很多小伙伴遇到了不知道无从下手,本篇以博客园的编辑器为例,解决如何定位富文本,输入文本内容 一.加载配置 1.打开博客园写随笔,首先需要登录,这里为了 ...
- web模拟telnet
https://blog.csdn.net/midion9/article/details/51781402 https://blog.csdn.net/hackstoic/article/detai ...
- hdu5790
都快忘了在这类题的经典做法了…… 将字符串一个个的插入字典树,在字典树维护好有该前缀串s的最大编号字符串j,我们记作j控制了前缀串s 对于当前的第i个字符串,维护此时有当前每个字符串控制了多少个前缀串 ...
- [libGDX游戏开发教程]使用Libgdx进行游戏开发(5)-关卡加载
在上一章我们介绍了如何管理和利用素材,但是我们注意到,这些素材都是零散的,比如岩石的左部等,这一章,我们将利用这些零件拼合成完整的游戏对象. 回顾最开始的设计类图,注意Level类和所有Level中的 ...
- nodejs获取ASP.Net WebAPI(IIS Windows验证)
处理了很多天,终于使用Nodejs可以发出请求至WebAPI,能够正常处理数据了 首先加入npm包 npm install httpntlm 在app.js中加入代码 var httpntlm = r ...
- HDU 2280 Tetris Comes Back
状态压缩,$dp$,预处理. 设$dp[i][j]$为前$i-1$行填满,第$i$行为状态$j$的最小需要$1$种类的数量.预处理好每种状态能推出哪些状态,并且记录下所需花费就可以了. #pragma ...
- JNI基础学习
1.JNI(Java Native Interface): 它允许Java代码和其他语言写的代码进行交互,JNI一开始是为了本地已编译语言,尤其是C和C++而设计的,但是它并不妨碍你使用其他语言,只要 ...
- Proxy(2016山东省省赛C)(最短路)(spfa)
问题 C: Proxy 时间限制: 2 Sec 内存限制: 128 MB提交: 17 解决: 5[提交][状态][讨论版] 题目描述 Because of the GFW (Great Firew ...
- [BZOJ4650][NOI2016]优秀的拆分(SAM构建SA)
关于解法这个讲的很清楚了,主要用了设关键点的巧妙思想. 主要想说的是一个刚学的方法:通过后缀自动机建立后缀树,再转成后缀数组. 后缀数组功能强大,但是最令人头疼的地方是模板太难背容易写错.用这个方法, ...