一步步分析从请求到响应涉及到 Rails 的哪些模块
纯 Rack 实现
# config.ru
require 'bundler/setup'
run Proc.new {|env|
if env["PATH_INFO"] == "/"
[200, {"Content-Type" => "text/html"}, ["<h1>Hello World</h1>"]]
else
[404, {"Content-Type" => "text/html"}, ["<h1>Not Found</h1>"]]
end
}引入 Action Dispatch & 纯手动实现 Controller#actions
# config.ru
require 'bundler/setup'
require 'action_dispatch'
routes = ActionDispatch::Routing::RouteSet.new
routes.draw do
get '/' => 'mainpage#index'
# 和以下写法效果一样,但'这里'要把它定义在 MainpageController 后面
# get '/' => MainpageController.action("index")
get '/page/:id' => 'mainpage#show'
end
class MainpageController
def self.action(method)
controller = self.new
controller.method(method.to_sym)
end
def index(env)
[200, {"Content-Type" => "text/html"}, ["<h1>Front Page</h1>"]]
end
def show(env)
[200, {"Content-Type" => "text/html"},
["<pre> #{env['action_dispatch.request.path_parameters'][:id]} #</pre>"]]
end
end
run routes引入 Action Controller,使用 Metal
引用 Metal 增强模块 & Controller 里纯手工打造 View 渲染相关代码
引进 Action View
直接使用 ActionController::Base
其它
最后更新于