- 浏览: 2690668 次
- 性别:
- 来自: 北京
-
文章分类
最新评论
-
80后的童年2:
深入浅出MongoDB应用实战开发网盘地址:https://p ...
MongoDB入门教程 -
shliujing:
楼主在不是精通java和php的前提下,请不要妄下结论。
PHP、CakePHP哪凉快哪呆着去 -
安静听歌:
希望可以一给一点点注释
MySQL存储过程之代码块、条件控制、迭代 -
qq287767957:
PHP是全宇宙最强的语言!
PHP、CakePHP哪凉快哪呆着去 -
rryymmoK:
深入浅出MongoDB应用实战开发百度网盘下载:链接:http ...
MongoDB入门教程
1,action_controller.rb:
和active_record.rb很类似,加载当前文件,加载activesupport,加载一坨一坨的组件
2,base.rb:
节选了部分代码,调用流程为process -> perform_action => action || method_missing || template || raise UnknownAction
其中需要注意的几点:
1,@@default_charset = "utf-8"
2,attr_internal的属性有request、params、response、session、headers
3,cattr_accessor的属性有default_charset、logger、consider_all_requests_local等
catter_accessor标识的是Class Attributes,见这篇BlogSo, cattr_accessor doesn’t work like it should?
catter_accessor方法的定义在active_support\core_ext\class\attribute_accessors.rb:
4,render方法的参数有:
1)nil -> render_file
2):text -> render_text
3):file -> render_file
4):template -> render_file
5):inline -> render_template
6):action -> render_action
7):xml -> render_xml
8):json -> render_json
9):partial -> render_partial || render_partial_collection
10):update -> render_javascript
11):noting -> render_text
5,redirect_to方法的参数有:
1)Hash -> redirect_to(url_for(options))
2)URL -> response.redirect(options)
3)String -> redirect_to(request.protocol + request.host_with_port + options)
4):back -> redirect_to(request.env["HTTP_REFERER"])
我不太理解self.view_class,首先关键字returning没见过,我没理解template_class的意思,response.template为什么不直接用
ActiveView::Base.new()来构造,希望能够帮忙解惑,谢谢了!
$:.unshift(File.dirname(__FILE__)) unless $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__))) unless defined?(ActiveSupport) begin $:.unshift(File.dirname(__FILE__) + "/../../activesupport/lib") require 'active_support' rescue LoadError require 'rubygems' gem 'activesupport' end end require 'action_controller/base' require 'action_controller/deprecated_redirects' require 'action_controller/request' require 'action_controller/deprecated_request_methods' require 'action_controller/rescue' require 'action_controller/benchmarking' require 'action_controller/flash' require 'action_controller/filters' require 'action_controller/layout' require 'action_controller/deprecated_dependencies' require 'action_controller/mime_responds' require 'action_controller/pagination' require 'action_controller/scaffolding' require 'action_controller/helpers' require 'action_controller/cookies' require 'action_controller/cgi_process' require 'action_controller/caching' require 'action_controller/verification' require 'action_controller/streaming' require 'action_controller/session_management' require 'action_controller/components' require 'action_controller/macros/auto_complete' require 'action_controller/macros/in_place_editing' require 'action_view' ActionController::Base.template_class = ActionView::Base ActionController::Base.class_eval do include ActionController::Flash include ActionController::Filters include ActionController::Layout include ActionController::Benchmarking include ActionController::Rescue include ActionController::Dependencies include ActionController::MimeResponds include ActionController::Pagination include ActionController::Scaffolding include ActionController::Helpers include ActionController::Cookies include ActionController::Caching include ActionController::Verification include ActionController::Streaming include ActionController::SessionManagement include ActionController::Components include ActionController::Macros::AutoComplete include ActionController::Macros::InPlaceEditing end
和active_record.rb很类似,加载当前文件,加载activesupport,加载一坨一坨的组件
2,base.rb:
require 'action_controller/mime_type' require 'action_controller/request' require 'action_controller/response' require 'action_controller/routing' require 'action_controller/resources' require 'action_controller/url_rewriter' require 'action_controller/status_codes' require 'drb' require 'set' module ActionController class Base DEFAULT_RENDER_STATUS_CODE = "200 OK" include StatusCodes @@default_charset = "utf-8" cattr_accessor :default_charset class_inheritable_accessor :template_root cattr_accessor :logger attr_internal :request attr_internal :params attr_internal :response attr_internal :session attr_internal :headers attr_accessor :action_name class << self def controller_class_name @controller_class_name ||= name.demodulize end def controller_name @controller_name ||= controller_class_name.sub(/Controller$/, '').underscore end end public def process(request, response, method = :perform_action, *arguments) 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 ensure process_cleanup end def url_for(options = {}, *parameters_for_method_reference) case options when String options when Symbol ActiveSupport::Deprecation.warn( "You called url_for(:#{options}), which is a deprecated API call. Instead you should use the named " + "route directly, like #{options}(). Using symbols and parameters with url_for will be removed from Rails 2.0.", caller ) send(options, *parameters_for_method_reference) when Hash @url.rewrite(rewrite_options(options)) end end def controller_class_name self.class.controller_class_name end def controller_name self.class.controller_name end def session_enabled? request.session_options && request.session_options[:disabled] != false end protected def render(options = nil, deprecated_status = nil, &block) raise DoubleRenderError, "Can only render or redirect once per action" if performed? if options.nil? return render_file(default_template_name, deprecated_status, true) else unless options.is_a?(Hash) if options == :update options = { :update => true } else ActiveSupport::Deprecation.warn( "You called render('#{options}'), which is a deprecated API call. Instead you use " + "render :file => #{options}. Calling render with just a string will be removed from Rails 2.0.", caller ) return render_file(options, deprecated_status, true) end end end if content_type = options[:content_type] response.content_type = content_type.to_s end if text = options[:text] render_text(text, options[:status]) else if file = options[:file] render_file(file, options[:status], options[:use_full_path], options[:locals] || {}) elsif template = options[:template] render_file(template, options[:status], true) elsif inline = options[:inline] render_template(inline, options[:status], options[:type], options[:locals] || {}) elsif action_name = options[:action] ActiveSupport::Deprecation.silence do render_action(action_name, options[:status], options[:layout]) end elsif xml = options[:xml] render_xml(xml, options[:status]) elsif json = options[:json] render_json(json, options[:callback], options[:status]) elsif partial = options[:partial] partial = default_template_name if partial == true if collection = options[:collection] render_partial_collection(partial, collection, options[:spacer_template], options[:locals], options[:status]) else render_partial(partial, ActionView::Base::ObjectWrapper.new(options[:object]), options[:locals], options[:status]) end elsif options[:update] add_variables_to_assigns @template.send :evaluate_assigns generator = ActionView::Helpers::PrototypeHelper::JavaScriptGenerator.new(@template, &block) render_javascript(generator.to_s) elsif options[:nothing] render_text(" ", options[:status]) else render_file(default_template_name, options[:status], true) end end end def render_action(action_name, status = nil, with_layout = true) template = default_template_name(action_name.to_s) if with_layout && !template_exempt_from_layout?(template) render_with_layout(:file => template, :status => status, :use_full_path => true, :layout => true) else render_without_layout(:file => template, :status => status, :use_full_path => true) end end def render_file(template_path, status = nil, use_full_path = false, locals = {}) add_variables_to_assigns assert_existence_of_template_file(template_path) if use_full_path logger.info("Rendering #{template_path}" + (status ? " (#{status})" : '')) if logger render_text(@template.render_file(template_path, use_full_path, locals), status) end def render_template(template, status = nil, type = :rhtml, local_assigns = {}) add_variables_to_assigns render_text(@template.render_template(type, template, nil, local_assigns), status) end def render_text(text = nil, status = nil, append_response = false) @performed_render = true response.headers['Status'] = interpret_status(status || DEFAULT_RENDER_STATUS_CODE) if append_response response.body ||= '' response.body << text else response.body = text end end def render_javascript(javascript, status = nil, append_response = true) response.content_type = Mime::JS render_text(javascript, status, append_response) end def render_xml(xml, status = nil) response.content_type = Mime::XML render_text(xml, status) end def render_json(json, callback = nil, status = nil) json = "#{callback}(#{json})" unless callback.blank? response.content_type = Mime::JSON render_text(json, status) end def render_nothing(status = nil) render_text(' ', status) end def render_partial(partial_path = default_template_name, object = nil, local_assigns = nil, status = nil) add_variables_to_assigns render_text(@template.render_partial(partial_path, object, local_assigns), status) end def render_partial_collection(partial_name, collection, partial_spacer_template = nil, local_assigns = nil, status = nil) add_variables_to_assigns render_text(@template.render_partial_collection(partial_name, collection, partial_spacer_template, local_assigns), status) end def render_with_layout(template_name = default_template_name, status = nil, layout = nil) render_with_a_layout(template_name, status, layout) end def render_without_layout(template_name = default_template_name, status = nil) render_with_no_layout(template_name, status) end def head(*args) if args.length > 2 raise ArgumentError, "too many arguments to head" elsif args.empty? raise ArgumentError, "too few arguments to head" elsif args.length == 2 status = args.shift options = args.shift elsif args.first.is_a?(Hash) options = args.first else status = args.first options = {} end raise ArgumentError, "head requires an options hash" if !options.is_a?(Hash) status = interpret_status(status || options.delete(:status) || :ok) options.each do |key, value| headers[key.to_s.dasherize.split(/-/).map { |v| v.capitalize }.join("-")] = value.to_s end render :nothing => true, :status => status end def erase_render_results response.body = nil @performed_render = false end def erase_redirect_results @performed_redirect = false response.redirected_to = nil response.redirected_to_method_params = nil response.headers['Status'] = DEFAULT_RENDER_STATUS_CODE response.headers.delete('Location') end def erase_results erase_render_results erase_redirect_results end def redirect_to(options = {}, *parameters_for_method_reference) case options when %r{^\w+://.*} raise DoubleRenderError if performed? logger.info("Redirected to #{options}") if logger response.redirect(options) response.redirected_to = options @performed_redirect = true when String redirect_to(request.protocol + request.host_with_port + options) when :back request.env["HTTP_REFERER"] ? redirect_to(request.env["HTTP_REFERER"]) : raise(RedirectBackError) else if parameters_for_method_reference.empty? redirect_to(url_for(options)) response.redirected_to = options else redirect_to(url_for(options, *parameters_for_method_reference)) response.redirected_to, response.redirected_to_method_params = options, parameters_for_method_reference end end end def expires_in(seconds, options = {}) cache_options = { 'max-age' => seconds, 'private' => true }.symbolize_keys.merge!(options.symbolize_keys) cache_options.delete_if { |k,v| v.nil? or v == false } cache_control = cache_options.map{ |k,v| v == true ? k.to_s : "#{k.to_s}=#{v.to_s}"} response.headers["Cache-Control"] = cache_control.join(', ') end def expires_now response.headers["Cache-Control"] = "no-cache" end def reset_session request.reset_session @_session = request.session response.session = @_session end private def perform_action if self.class.action_methods.include?(action_name) send(action_name) render unless performed? elsif respond_to? :method_missing send(:method_missing, action_name) render unless performed? elsif template_exists? && template_public? render else raise UnknownAction, "No action responded to #{action_name}", caller end end end end
节选了部分代码,调用流程为process -> perform_action => action || method_missing || template || raise UnknownAction
其中需要注意的几点:
1,@@default_charset = "utf-8"
2,attr_internal的属性有request、params、response、session、headers
3,cattr_accessor的属性有default_charset、logger、consider_all_requests_local等
catter_accessor标识的是Class Attributes,见这篇BlogSo, cattr_accessor doesn’t work like it should?
catter_accessor方法的定义在active_support\core_ext\class\attribute_accessors.rb:
class Class def cattr_reader(*syms) syms.flatten.each do |sym| next if sym.is_a?(Hash) class_eval(<<-EOS, __FILE__, __LINE__) unless defined? @@#{sym} @@#{sym} = nil end def self.#{sym} @@#{sym} end def #{sym} @@#{sym} end EOS end end def cattr_writer(*syms) options = syms.last.is_a?(Hash) ? syms.pop : {} syms.flatten.each do |sym| class_eval(<<-EOS, __FILE__, __LINE__) unless defined? @@#{sym} @@#{sym} = nil end def self.#{sym}=(obj) @@#{sym} = obj end #{" def #{sym}=(obj) @@#{sym} = obj end " unless options[:instance_writer] == false } EOS end end def cattr_accessor(*syms) cattr_reader(*syms) cattr_writer(*syms) end end
4,render方法的参数有:
1)nil -> render_file
2):text -> render_text
3):file -> render_file
4):template -> render_file
5):inline -> render_template
6):action -> render_action
7):xml -> render_xml
8):json -> render_json
9):partial -> render_partial || render_partial_collection
10):update -> render_javascript
11):noting -> render_text
5,redirect_to方法的参数有:
1)Hash -> redirect_to(url_for(options))
redirect_to :action => "show", :id => 5
2)URL -> response.redirect(options)
redirect_to "http://www.rubyonrails.org"
3)String -> redirect_to(request.protocol + request.host_with_port + options)
redirect_to "/images/screenshot.jpg"
4):back -> redirect_to(request.env["HTTP_REFERER"])
redirect_to :back
评论
1 楼
wuxu1314
2007-10-07
def self.view_class [color=blue]@view_class ||=[/color] # create a new class based on the default template class and include helper methods [color=red]returning[/color] Class.new(ActionView::Base) do |view_class| view_class.send(:include, master_helper_module) end end def initialize_template_class(response) raise "You must assign a template class through ActionController.template_class= before processing a request" unless @@template_class response.template = [color=blue]self.class.view_class.new[/color](self.class.view_root, {}, self) response.redirected_to = nil @performed_render = @performed_redirect = false end
我不太理解self.view_class,首先关键字returning没见过,我没理解template_class的意思,response.template为什么不直接用
ActiveView::Base.new()来构造,希望能够帮忙解惑,谢谢了!
发表评论
-
用了TextMate才知道什么叫神级Editor
2011-03-09 04:51 58020一直用Eclipse作为开发Ruby和Java项目的IDE,但 ... -
Ruby使用OAuth登录新浪微博和豆瓣
2011-01-09 12:49 4495首先需要安装oauth这个gem包 gem install ... -
使用Passenger+nginx部署Rails
2010-12-28 15:12 50551. Install Passender gem instal ... -
markItUp+rdiscount搭建Rails下可视化Markdown编辑器
2010-12-21 17:48 5491markItUp是基于jQuery的可视化编辑器,支持Html ... -
Rails3 and MongoDB Quick Guide
2010-12-10 14:13 2773Install MongoDB Download: http: ... -
基于ruby-protobuf的rpc示例
2009-08-11 11:51 41671, 安装ruby-protobuf gem instal ... -
Ruby导出xls和csv的utf-8问题的解决
2009-02-04 15:05 6878数据库数据为utf-8格式,包括中文和拉丁文等等 导出文件xl ... -
URL/HTML/JavaScript的encode/escape
2009-01-04 13:03 9373最近经常被URL、HTML、JavaScript的encode ... -
各种排序的Ruby实现
2008-11-27 14:51 4020Θ(n^2) 1, Bubble sort def bu ... -
12月5日北京RoR活动!
2008-11-26 18:38 3029又是一年过去了,Rails在国内的发展势态良好,很多使用RoR ... -
Rails程序开发的最大问题是代码规范
2008-08-28 11:56 5656使用Rails开发大型复杂B2B应用一年了,这个项目目前开发人 ... -
Web开发大全:ROR版——推荐序
2008-07-09 00:39 2438来自http://www.beyondrails.com/bl ... -
深入ActionMailer,使用Sendmail发邮件
2008-07-03 11:41 3408来自: http://www.beyondrails.com/ ... -
Rails里如何结合ExceptionNotification配置gmail账户发邮件
2008-06-19 19:56 31241,安装ExceptionNotification rub ... -
使用coderay和railscasts样式进行代码高亮
2008-06-17 00:16 2414CodeRay是一个语法高亮的Ruby库,效率很不错。 Cod ... -
Capistrano试用
2008-06-16 19:05 19741,客户端机器安装Capistrano gem insta ... -
lighttpd真垃圾啊
2008-06-04 18:38 2562使用lighttpd+fcgi跑Rails程序,文件上传会si ... -
将gem变成plugin
2008-06-04 11:27 1826有什么样的需求就有什么样的对策 当vhost上的帐号没有ge ... -
在Rails里使用ReCaptcha添加验证码
2008-06-03 15:51 42891,去http://recaptcha.net/sign up ... -
Rails里给文件上传添加progress_bar
2008-05-27 17:00 2113文件上传很慢时,UI没有什么用户提示,这样让人很费解,所以我们 ...
相关推荐
在Ruby on Rails(Rails)框架中,上传和转换视频是一个常见的需求,特别是在构建涉及多媒体内容的Web应用时。Rails提供了一系列工具和库,使得开发者能够方便地处理这种任务。本篇文章将详细介绍如何在Rails中实现...
在Ruby on Rails框架中,Paperclip是一个非常流行的用于处理文件上传的库。它提供了一种简单而优雅的方式来管理和处理模型中的附件,如图片、文档等。Paperclip与ActiveRecord紧密集成,使得在Rails应用中添加文件...
在Rails框架中,AJAX(Asynchronous JavaScript and XML)是一种常用的技术,用于创建动态和交互式的Web应用程序。AJAX允许页面在不刷新整个页面的情况下与服务器进行通信,从而提高用户体验。在本篇博文中,我们将...
《Rails 4 in Action》是一本深入探讨Ruby on Rails框架的权威书籍,旨在帮助开发者全面理解Rails 4的精髓和实践。"r4ia_examples"是该书的配套代码库,包含了书中各个章节示例的源码,为读者提供了亲自动手实践的...
总结,Rails的邮件支持通过Action Mailer提供了一种直观且强大的方式来处理邮件发送。开发者可以轻松地创建Mailer类,定义邮件方法,使用模板定制邮件内容,并通过配置SMTP服务器实现实际发送。此外,邮件预览功能为...
Ruby on Rails是一个流行的开源Web开发框架,它以其MVC(模型-视图-控制器)架构、DRY(Don't Repeat Yourself)原则和元编程能力而闻名。SQLite则是一种轻量级、嵌入式的SQL数据库,常用于小型项目或开发环境,因为...
Jfinal 是一个MVC(Model-View-Controller)架构的实现,它借鉴了Ruby on Rails的许多设计理念,使得Java开发者也能体验到快速开发的乐趣。 在Jfina框架中,主要包含以下几个核心概念: 1. **Controller**:控制器...