Metal 使用举例
2015-07-20 20:13
411 查看
Metal 使用举例
原生的 Metal
在 Rails 里 metal 也属于 middleware,我们可以这么用:# config/routes.rb get 'hello' => 'hello#index' # ...
# app/controllers/hello_controller.rb class HelloController < ActionController::Metal def index self.response_body = "Hello World!" end end
然后浏览器访问:http://localhost:3000/hello 可以获取刚才的内容。
日志会由原来的:
Started GET "/hello" for 127.0.0.1 at 2014-04-27 09:04:49 +0800 Processing by HelloController#index as HTML Completed 200 OK in 1ms (ActiveRecord: 0.0ms)
变为:
Started GET "/hello" for 127.0.0.1 at 2014-04-27 08:57:07 +0800
加入 Rendering 模块
默认 ActionController::Metal 是没有提供渲染视图、模板和其它需要明确调用到 response_body=, content_type=, status= 的方法。如果你需要这些,可以引入它们:class HelloController < ActionController::Metal include AbstractController::Rendering include ActionView::Layouts append_view_path "#{Rails.root}/app/views" def index render "hello/index" end end
加入 Redirection 模块
想使用重定向相关代码,你也需要引入它们:class HelloController < ActionController::Metal include ActionController::Redirecting include Rails.application.routes.url_helpers def index redirect_to root_url end end
加入其它模块
参考 ActionController::Base 引入其它模块,以达到目的。class ApiController < ActionController::Metal # 使用 Metal, 而不是 Base include ActionController::Helpers include ActionController::Redirecting include ActionController::Rendering include ActionController::Renderers::All include ActionController::ConditionalGet # 需要响应 .json .xml 等不同格式的话,使用它。 include ActionController::MimeResponds include ActionController::RequestForgeryProtection # 如果你需要 SSL include ActionController::ForceSSL include AbstractController::Callbacks # 想要知道 Controller 处理过程中,性能这方面的数据。 include ActionController::Instrumentation # 需要转换 params 类型的话,可以使用它。 include ActionController::ParamsWrapper include Devise::Controllers::Helpers # 在项目里使用路由相关的 helper 方法。 include Rails.application.routes.url_helpers # 视图文件放在哪? append_view_path "#{Rails.root}/app/views" # 需要转换 params 类型的话,可以使用它。 # { "person": { "name": "Kelby", "email": "leekelby@gmail.com" }} wrap_parameters format: [:json] # 根据客户端决定是否需要。(另,如果客户端不是浏览器的话,它会自动跳过) protect_from_forgery end
上述代码,仅供参考。关于各模块及其作用,详情可以参考对应章节。
参考
Developing api with rails metal
用技术做兼职,找技术兼职,就上:
http://shixian.com/
相关文章推荐
- 怎么说呢。。。。。
- 非常好用的JQuery自动补全插件bigautocomplete
- 中小企业为什么要上HR系统
- hdu 3622 Bomb Game(二分答案+2-sat判断答案可行性)
- PopupWindow的使用
- 关于NSSet集合 anyObject的取值猜测
- HibernateTemplate的在操作数据库的一些方法
- Queue的使用
- 恶搞-Mac 让电脑说话
- MVC4新功能...压缩和合并js文件和样式文件
- UVA401 第一个编程语言博客纪念一下
- hdu 5285 wyh2000 and pupil
- ImageLoader配置(注释)
- Android onTouch事件与手势操作
- 欧拉函数简单应用
- 12天学好C语言——记录我的C语言学习之路(Day 5)
- Android系统启动过程(转载)
- 利用基于@AspectJ的AOP实现权限控制
- 12天学好C语言——记录我的C语言学习之路(Day 5)
- 用户与操作系统的接口是什么?一个分时系统提供什么接口?一个批处理系统又提供什么接口?