1)session的使用:
赋值:session[:user_id]=@user.id
取值:logger.infosession[:user_id]
注意:将controller里面protect_from_forgery语句去掉,使能session。session也是Hash对象--key=>value,我们通过key[:user_id]找到对应的value[126]
2)查询:
find:调用类名时,必须通过ID查询;
where:用于对其他字段进行查询
3)报表生成:
Echarts:http://echarts.baidu.com/index.html
4)rails控制台启动:
$rails c
irb>ps -aux |grep rails#打印当前rails进程信息
irb>kill-9xxx #强制杀死进程xxx
5)按时间查询记录
q_time= Time.parse params[:p_time]
<time.Parse("01/02/2006","02/08/2015")#从字符串转为时间戳,第一个参数是格式第二个是要转换的时间字符串>
@docs = Doc.where("p_time<=?",q_time) #在Doc表里查询p_time<=参数为q_time的记录
6)设置时区
config/application.rb:
# config.time_zone = 'Central Time (US &Canada)' (在该句下面插入以下语句)
config.time_zone = 'Beijing'(新增语句)
config.active_record.default_timezone =:Beijing
7)rails数据验证
Rubyon railsguides:http://guides.rubyonrails.org/active_record_validations.html
8)用户访问控制
a>app/controllers/application_controller.rb中添加如下代码:
classApplicationController < ActionController::Base
before_filter:authorize
protect_from_forgery
protected
def authorize
unlessUser.find_by_id(session[:user_id])
redirect_to'/user/index'
end
end
end
b>app/controllers/user_controller.rb:
首行代码添加:skip_before_filter:authorize
<b>修改于第四周周五</b>