Ruby on rails 新手学习第五天

Ruby on rails 新手学习第五天

Controller: actionpack gem, ActionController::Base
app/controllers目录
命名规则
支持命名空间,以module的方式组织:
Ruby on rails 新手学习第五天
Instance Methods in Controller:
params:获取HTTP请求中GET/POST参数 (params[:user] || params[“user”])
session & cookies
render & redirect_to
send_data & send_file: 向浏览器返回可供下载或展示的二进制数据
Ruby on rails 新手学习第五天
request
request.fullpath:返回当前HTTP请求的路径
request.get?:判断当前的请求是否是get方法
request.headers:返回request的所有头部信息的hash格式
request.ip:获取用户端的ip地址
request.body:获取HTTP请求中body放置数据

response
response.location:设置当前HTTP response location 的header
response.response_code:当前返回的状态码
response.body:设置response的body

Class Methods in Controller:
Filters
before_action
after_action
around_action

CSRF
protect_from_forgery

.helper_method
在controller定义函数后,放在helper_method中,view就可以使用这些方法
Ruby on rails 新手学习第五天
Rails.logger
Rails.logger.info “debug info”
Rails.logger.info “Exception”
Exception
rescure_from:定义项目中所有可能出现的异常,抓取后暴露出用户可读的错误信息
Ruby on rails 新手学习第五天
不仅告诉用户发生了什么,还用邮件通知开发人员
Ruby on rails 新手学习第五天