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 ...
随机推荐
- 使用echarts展示线状图信息的时候数据部分数据因为x轴的数据显示不全而隐藏的问题
在使用echarts来展示数据时,因为数据很多的原因导致x轴显示不全,然后有些数据也隐藏在图表中,所以这个时候我们要在 series 中设置一个属性,让所有的数据都能够展示出来,这里我们需要添加的属性 ...
- 使用chardet模块判断网页编码
import chardet import urllib.request url='http://stock.sohu.com/news/' html = urllib.request.urlopen ...
- hdu 2215 & hdu 3932(最小覆盖圆)
Maple trees Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- 看不到Harbor我也睡不着觉啊
上午打球,下午陪小孩子看上海科技展,晚上搞定harbor. 完美!!!:) 参考文档: https://www.dwhd.org/20161023_110618.html http://blog.cs ...
- 打印sql语句方法
var_dump($this->blackpool_model->getLastSql());
- VisualStudio 2013开发Office插件
在VS中选择创建新项目,选择App for Office 选择mail出现的位置 Task pane The app appears in the task pane of a Microsift O ...
- $_ENV 为空的原因
php.ini里面的variables_order的值为GPCS,修改为EGPCS,然后重启wamp即可. print_r($_ENV); var_dump($_ENV); foreach ($_EN ...
- HDU 6362.oval-and-rectangle-数学期望、微积分 (2018 Multi-University Training Contest 6 1001)
2018 Multi-University Training Contest 6 6362.oval-and-rectangle 题意就是椭圆里画内接矩形,问你矩形周长的期望. 比赛的时候推了公式,但 ...
- POJ 2492 A Bug's Life【并查集高级应用+类似食物链】
Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes ...
- 51nod 1129 字符串最大值
首先我们可以想到的是,既然求的是前缀的长度,就意味着一定是从1开始的,那么我们可以直接用下 标表示每一个前缀.但是可能存在几个前缀互相包含的情况,比如:abababa我们可以看见的是aba中包含着ab ...