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 ...
随机推荐
- mybatis获取表信息,以及遍历ResultSet
@RunWith(SpringRunner.class) @SpringBootTest public class BravolinksCrmServerApplicationTests { @Aut ...
- [ Openstack ] Openstack-Mitaka 高可用之 环境初始化
目录 Openstack-Mitaka 高可用之 概述 Openstack-Mitaka 高可用之 环境初始化 Openstack-Mitaka 高可用之 Mariadb-Galera集群 ...
- [ LDAP ] LDAP服务搭建及应用
ldap 搭建及应用 node1: 192.168.118.14node2: 192.168.118.25 ldap server : 192.168.118.14 1. 安装LDAP服务器 [roo ...
- jquery 操作dom效率测试------html和append插入文档
$(function () { var htmlResult = createHtmlContent(100); console.log(htmlResult) insertHtml.call($(& ...
- 读取pandas修改单列数据类型
import pandas as pd import numpy as np df = pd.read_csv('000917.csv',encoding='gbk') df = df[df['涨跌幅 ...
- k8s通过label来控制pod的位置
默认情况下,scheduler会将pod调度到所有可用的Node,不过有些情况我们希望将 Pod 部署到指定的 Node,比如将有大量磁盘 I/O 的 Pod 部署到配置了 SSD 的 Node:或者 ...
- C#在 64位系统下出现 “未能加载文件或程序集”错误
64位系统下,Build的时候,如果选择Any CPU,默认会按照64位进行编译,便无法加载某些旧的dll,这些dll可能是特定到X86 CPU的. 所以,把编译选项中改为 X86CPU,就可以运行了 ...
- 使用base64对图片的二进制进行编码,使其可以利用ajax进行显示
有时候我们需要动态的将图片的二进制在页面上进行显示,如我们需要弄一个验证码的功能,那么如果我们的验证码的图片在后台得到的是该图片的二进制,那么当我们需要在页面上点击一个按钮利用ajax进行切换的时候, ...
- 计蒜客 30990.An Olympian Math Problem-数学公式题 (ACM-ICPC 2018 南京赛区网络预赛 A)
A. An Olympian Math Problem 54.28% 1000ms 65536K Alice, a student of grade 66, is thinking about a ...
- POJ2032 Building a Space Station(Kruskal)(并查集)
Building a Space Station Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 7469 Accepte ...