ruby on rails学习笔记(一)

来源:互联网 发布:炮萝数据 编辑:程序博客网 时间:2024/05/01 22:10
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