Rails 使用 学习 笔记

备注: 所有以下命令均在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则反之。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值