这里简要看看核心代码:
- # Extracts the action_name from the request parameters and performs that action.
- def process(request, response, method = :perform_action, *arguments) #:nodoc:
- initialize_template_class(response)
- assign_shortcuts(request, response)
- initialize_current_url
- assign_names
- forget_variables_added_to_assigns
- log_processing
- send(method, *arguments)
- assign_default_content_type_and_charset
- response.request = request
- response.prepare! unless component_request?
- response
- ensure
- process_cleanup
- end
- def initialize_template_class(response)
- response.template = ActionView::Base.new(self.class.view_paths, {}, self)
- response.template.extend self.class.master_helper_module
- response.redirected_to = nil
- @performed_render = @performed_redirect = false
- end
2) assign_shortcuts
主要是
定义快捷访问request和response内部变量的一些全局的类变量
flash的处理也是hook到这个方法的(后面再写这个)
- def assign_shortcuts(request, response)
- @_request, @_params, @_cookies = request, request.parameters, request.cookies
- @_response = response
- @_response.session = request.session
- @_session = @_response.session
- @template = @_response.template
- @assigns = @_response.template.assigns
- @_headers = @_response.headers
- end
4)
forget_variables_added_to_assigns 设置变量标记为未设置,assign_names 就是设置@action_name
5)log_processing
6)send(method, *arguments)就是调用method(默认是perform_action)
action_name就是assign_names设置的值
- def perform_action
- if self.class.action_methods.include?(action_name)
- send(action_name)
- default_render unless performed?
- elsif respond_to? :method_missing
- method_missing action_name
- default_render unless performed?
- elsif template_exists? && template_public?
- default_render
- else
- raise UnknownAction, "No action responded to #{action_name}", caller
- end
- end
如果 method_missing 执行找不到方法的相关操作
如果 存在模板直接render
最后抛出UnknownAction异常
7) assign_default_content_type_and_charset
- def assign_default_content_type_and_charset
- response.content_type ||= Mime::HTML
- response.charset ||= self.class.default_charset unless sending_file?
- end
response.request = request
response.prepare!
最后返回
response,由前面讲的Dispacher执行response.out(@output)