Rails 使用 学习 笔记
2013-05-07 00:50
323 查看
备注: 所有以下命令均在Rails3 版本以上,3以下命令有所不同
1.生成新的web project
rails [projectName]
2.启动你的Web Project
rails server 或者 rails s
3.生成你的支架
rails g scaffold ticket name:string seat_id_seq:string address:text price_paid:decimal email_address:string 【其中g是generate的简写,当然你也可以使用generate】
4.访问的的web project
http://127.0.0.1:3000/ 以及 http://127.0.0.1:3000/tickets
5.这个时候你会发现 后面一个站点上会有错误,是由于我们在第三步生成的支架,定义了表结构,但是还未正真的创建表。
需要使用rails的迁移来生成数据库表,查看db/migrate文件夹,你好发现一个加了时间戳的 .rb文件 这个文件就是在创建支架式rails自动生成的数据库创建脚本,rails遵循一个原则,DRY, don't repeat yourself。 你在创建任何一个系统相关的模块时,相关的模块脚本也会为你创建,但在仅增加部分结构式例外,如单个增加字段,或者操作方式(增删改查任何一种)。
执行rake,生成数据库表
rake db:migrate
此时再回到http://127.0.0.1:3000/tickets,一个完整的增删改查的也页面通过rails的几行命令就实现了。速度快吧。
Yeah, Enjoy Rails!
2013-05-08 昨天雷雨黄色警报,今天暴雨。
Tips:
在创建Rails数据结构时,切记,大小写时sensitive的!
2013-05-17 晴
问题:layout中的application.html.erb,在什么情况下才会被使用,我在用scaffold生成的代码中可以自动使用这个layout,但是手动加的,比如home/index.html.erb并不会使用这个模板,为什么,哪里会有什么设置。
2013-05-20 晴
flash.now与flash区别:
There’s a subtle difference between flash and flash.now. The flash variable is
designed to be used before a redirect, and it persists on the resulting page for one
request---that is, it appears once, and disappears when you click on another link. Unfortunately,
this means that if we don’t redirect, and instead simply render a page (as
in Listing 9.8), the flash message persists for two requests: it appears on the rendered
page but is still waiting for a ‘‘redirect’’ (i.e., a second request), and thus appears again
if you click a link.
To avoid this weird behavior, when rendering rather than redirecting we use
flash.now instead of flash. The flash.now object is specifically designed for
displaying flash messages on rendered pages. If you ever find yourself wondering
why a flash message is showing up where you don’t expect it, chances are good that
you need to replace flash with flash.now.
关于nil在find及find_by_columnname中使用区别
可以这么理解,find会返回一个对象数组,而find_by_columnname会返回一个对象,而如果在两者查询都没有结果集的情况下,find会返回一个空对象数组(注意不是空,及非nil),而find_by_columnname会返回一个空对象,如:
guser = User.find(:all, condition=>["username = '11111' "])
suser = User,find_by_username("111111")
如果此时用nil判断两个返回对象的值
表达式 {suser.nil?} 的值为true
而 {guser.nil?} 的值为false
也就是说不过查询是否有结果集,find的结果始终不会为nil,而find则反之。
1.生成新的web project
rails [projectName]
2.启动你的Web Project
rails server 或者 rails s
3.生成你的支架
rails g scaffold ticket name:string seat_id_seq:string address:text price_paid:decimal email_address:string 【其中g是generate的简写,当然你也可以使用generate】
4.访问的的web project
http://127.0.0.1:3000/ 以及 http://127.0.0.1:3000/tickets
5.这个时候你会发现 后面一个站点上会有错误,是由于我们在第三步生成的支架,定义了表结构,但是还未正真的创建表。
需要使用rails的迁移来生成数据库表,查看db/migrate文件夹,你好发现一个加了时间戳的 .rb文件 这个文件就是在创建支架式rails自动生成的数据库创建脚本,rails遵循一个原则,DRY, don't repeat yourself。 你在创建任何一个系统相关的模块时,相关的模块脚本也会为你创建,但在仅增加部分结构式例外,如单个增加字段,或者操作方式(增删改查任何一种)。
执行rake,生成数据库表
rake db:migrate
此时再回到http://127.0.0.1:3000/tickets,一个完整的增删改查的也页面通过rails的几行命令就实现了。速度快吧。
Yeah, Enjoy Rails!
2013-05-08 昨天雷雨黄色警报,今天暴雨。
Tips:
在创建Rails数据结构时,切记,大小写时sensitive的!
2013-05-17 晴
问题:layout中的application.html.erb,在什么情况下才会被使用,我在用scaffold生成的代码中可以自动使用这个layout,但是手动加的,比如home/index.html.erb并不会使用这个模板,为什么,哪里会有什么设置。
2013-05-20 晴
flash.now与flash区别:
There’s a subtle difference between flash and flash.now. The flash variable is
designed to be used before a redirect, and it persists on the resulting page for one
request---that is, it appears once, and disappears when you click on another link. Unfortunately,
this means that if we don’t redirect, and instead simply render a page (as
in Listing 9.8), the flash message persists for two requests: it appears on the rendered
page but is still waiting for a ‘‘redirect’’ (i.e., a second request), and thus appears again
if you click a link.
To avoid this weird behavior, when rendering rather than redirecting we use
flash.now instead of flash. The flash.now object is specifically designed for
displaying flash messages on rendered pages. If you ever find yourself wondering
why a flash message is showing up where you don’t expect it, chances are good that
you need to replace flash with flash.now.
关于nil在find及find_by_columnname中使用区别
可以这么理解,find会返回一个对象数组,而find_by_columnname会返回一个对象,而如果在两者查询都没有结果集的情况下,find会返回一个空对象数组(注意不是空,及非nil),而find_by_columnname会返回一个空对象,如:
guser = User.find(:all, condition=>["username = '11111' "])
suser = User,find_by_username("111111")
如果此时用nil判断两个返回对象的值
表达式 {suser.nil?} 的值为true
而 {guser.nil?} 的值为false
也就是说不过查询是否有结果集,find的结果始终不会为nil,而find则反之。
相关文章推荐
- android 开发零起步学习笔记(十九):android使用全局变量的两种方法
- ssi开发 学习笔记第三天--配置使用SSI
- NodeJS学习系列课程笔记(NodeJS util工具类相关使用方法)
- Docker学习笔记六 使用Docker构建服务
- Unix学习笔记------文件IO------使用lseek设置文件当前读写偏移量(即:当前的读写位置)
- HTML学习笔记1.6-高亮文本及组合使用
- word的使用学习笔记(一)
- React-Native学习笔记之:使用Tab react-native-tab-navigator框架
- NDK学习笔记:动态链接库与静态链接库的基本使用流程简记
- MonoRail学习笔记十七:TransformFilter的使用
- WMI005-WMI学习笔记(五)——Using WMI with the .NET Framework(.NET框架使用WMI)
- cocos2d-x学习笔记(四)粒子系统使用中多次调用时GL calls不断增加的解决办法
- Liferay Portal 6学习笔记3:Liferay IDE的安装使用
- APIDemo学习笔记——Android上几种简单的Animation使用方法
- 学习笔记:UINavigationController使用详解
- C++学习笔记(第11章->使用类)
- spring in action 学习笔记(九)使用注解进行装配
- eclipse的几个使用技巧(我的java学习笔记)
- 学习笔记之使用vi之使用小技巧
- onvif学习笔记4:Windows环境使用gsoap生成onvif框架代码