ruby on rails学习笔记(一)

1.  Models in Rails use a singular name, and their correspondingdatabase tables use a plural name. For the model to hold comments,the convention is to use thename  Comment

之前从一无所知开始乱看rails代码,对于数据库表格用复数,而controller用单数一直不解。比如要为blog增加一个评论功能代码如下:
$ rails generate model Comment commenter:string body:textpost:references
这段代码创建了如下的一些文件:
db/migrate/20100207235629_create_comments.rb
app/models/comment.rb
test/unit/comment_test.rb
test/fixtures/comments.yml

里面的 model/comment.rb   是单数, 而生成的数据库名是comments.

2. 在ruby代码里添加css的class:
  %th{:class => 'hilite'}
在css文件里有这个:   th.hilite {  background-color : yellow; }

3. 如何使点一个表格上的一个列名而是该列按照大小排序:
首先要把这个列名, 比如 movie title 做成一个 link_to 超链接格式,指向controller的一个方法。 
例子如下: 
%= link_to 'movie title', title_path(:sort =>'title'), :id = 'title_header'   %
title_path(:sort=> 'title')的作用是向title这个controller传递一个参数 :sort, 然后controller里面的某个方法用params[:sort]获得那个参数以后,做相应的一个操作。   最后的 :id是定义一个cssid tag.

4. 之前那个排序的方法在controller里面, 针对一个table排序,可以想象应该是取得table所有元素,然后排序。 可以参考 rails API里的ActiveRecord, 用里面的 order 和all方法。   例子:
Movie.order(params[:sort]).all  

5. rails给数据库加一列数据,参考ActiveRecord::Migration,  http://api.rubyonrails.org/classes/ActiveRecord/Migration.html

6. OmniAuth gem:  第三方authorization 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值