{"id":4561,"date":"2010-06-08T23:32:51","date_gmt":"2010-06-08T15:32:51","guid":{"rendered":"http:\/\/ihower.tw\/blog\/?p=4561"},"modified":"2011-04-09T01:49:11","modified_gmt":"2011-04-08T17:49:11","slug":"rails3-metel","status":"publish","type":"post","link":"https:\/\/ihower.tw\/blog\/4561-rails3-metel","title":{"rendered":"Rails3: \u65b0\u7684 Metal \u6a5f\u5236"},"content":{"rendered":"<p>\u65b0\u7684 Rails3 Controller \u91cd\u69cb\u5f8c\uff0c\u8b8a\u6210 ActionController::Base < ActionController::Metal < AbstractController \u7684\u7e7c\u627f\u9ad4\u7cfb\u3002\u9664\u4e86 ActionMailer \u7e7c\u627f\u81ea AbstractController\uff0c\u8b93 API \u8207 Controller \u4e00\u81f4\u4e4b\u5916\uff0c\u65b0\u7684 <a href=\"http:\/\/www.engineyard.com\/blog\/2010\/rails-and-merb-merge-plugin-api-part-3-of-6\/\">ActionController::Metal<\/a> \u66f4\u662f\u5145\u6eff\u73a9\u5473\u3002<\/p>\n<p>\u6545\u4e8b\u53ef\u4ee5\u5f9e\u4e0a\u500b\u79ae\u62dc\uff0cYehuda \u628a <a href=\"http:\/\/weblog.rubyonrails.org\/2008\/12\/17\/introducing-rails-metal\">Rails 2 \u7684 Metal<\/a> \u79fb\u9664\u4e86(<a href=\"http:\/\/github.com\/rails\/rails\/commit\/45e60283e733a535d68d499aa20e095c905f43b0\">commit<\/a>)\uff0c\u6839\u64da commit \u7684\u8aaa\u660e\uff0cRails 2 \u7684 Metal \u5728 Rails 3 \u88e1\u9762\uff0c\u53ef\u4ee5\u7528 1. \u653e\u5728 Route \u4e4b\u524d\u7684 <a href=\"http:\/\/asciicasts.com\/episodes\/151-rack-middleware\">Rack middleware<\/a> \u6216 2. \u653e\u5728 Route \u4e4b\u5f8c\u7684\u4e00\u500b Rack endpoint\uff0c\u800c\u9019\u500b Rack endpoint\uff0c\u9664\u4e86\u81ea\u5df1\u5be6\u4f5c Rack app\uff0c\u6211\u5011\u4e5f\u53ef\u4ee5\u7528 ActionController::Metal \u4f86\u5be6\u4f5c\u51fa\u81ea\u5df1\u7684\u5ba2\u88fd\u5316 Controller\u3002<\/p>\n<p>Rails2 \u767c\u660e Metal \u539f\u56e0\u662f\uff0c\u6709\u4e9b\u4e8b\u60c5\u4e0d\u9700\u8981\u5b8c\u6574\u7684 Controller \u529f\u80fd\uff0c\u5e0c\u671b\u80fd\u5920\u8d8a\u5feb\u8d8a\u597d\uff0c\u4f8b\u5982 XML\/JSON API \u7b49\u3002\u800c Rails2 \u7684 Metal \u96d6\u7136\u975e\u5e38\u5feb\uff0c\u4f46\u662f\u6c92\u4ec0\u9ebc\u5f48\u6027\uff0c\u5b8c\u5168\u4e0d\u80fd\u4f7f\u7528 Controller \u7684\u529f\u80fd\uff0c\u4f8b\u5982\u7528 Layout, Template, Sessions \u7b49\uff0c\u57fa\u672c\u4e0a\u5c31\u8ddf\u55ae\u7d14\u7684 Rack middleware \u6c92\u4ec0\u9ebc\u5169\u6a23\u3002\u4f46\u662f\u5728 Rails3 \u4e2d\uff0c\u53ef\u4ee5\u900f\u904e\u81ea\u7e7c\u627f ActionController::Metal \u505a\u51fa\u767d\u7d19\u822c\u7684\u5ba2\u88fd Controller\uff0c\u53ef\u4ee5\u6709\u975e\u5e38\u5feb\u7684\u901f\u5ea6\uff0c\u5982\u679c\u6709\u9700\u8981\u7528\u5230 Controller \u7684\u529f\u80fd\uff0c\u4e5f\u53ef\u4ee5\u4efb\u610f\u9078\u64c7\u7d44\u5408\u52a0\u5165\uff0c\u5341\u5206\u5f48\u6027\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u6211\u5011\u4f86\u5be6\u4f5c\u4e00\u500b\u8d85\u7d1a\u7cbe\u7c21\u7684 Static Controller\uff1a<\/p>\n<pre>\r\n<code>\r\n# lib\/static_controller.rb\r\nclass StaticController < ActionController::Metal\r\n  include ActionController::Rendering\r\n    \r\n  append_view_path \"#{Rails.root}\/app\/views\"\r\n    \r\n  def about\r\n    render \"about\"\r\n  end\r\nend\r\n    \r\n# config\/route.rb\r\nmatch '\/about', :to => \"static#about\", :as => :about\r\n<\/code>\r\n<\/pre>\n<p>\u9019\u500b\u7bc4\u4f8b\u6709\u63a5\u8fd1\u65bc Metal \u7684\u901f\u5ea6\uff0c\u4e26\u52a0\u5165\u4e86 Controller \u7684 Template \u529f\u80fd (\u4f60\u53ef\u4ee5\u53c3\u8003 ActionController::Base \u9019\u662f\u64c1\u6709\u5168\u90e8\u529f\u80fd\u7684\u7248\u672c)\u3002\u5176\u4e2d &#8220;static#about&#8221; \u662f StaticController \u7684 about action \u7e2e\u5beb\uff0c\u800c\u5728 Rails3 \u4e2d\uff0ccontroller action \u4e5f\u90fd\u662f\u4e00\u500b Rack app\u3002<\/p>\n<p>\u9664\u4e86\u6211\u7684\u9019\u500b\u7c21\u55ae\u7bc4\u4f8b\uff0c\u5728 <a href=\"http:\/\/www.railsdispatch.com\/posts\/rails-routing\">The Powerful New Rails Router<\/a> \u548c <a href=\"http:\/\/www.railsdispatch.com\/posts\/upgrading-a-rails-2-app-to-rails-3\">Upgrading a Rails 2 App to Rails 3<\/a> \u7684\u5f71\u7247\u4e2d\uff0c\u4e5f\u6709\u5206\u5225\u8209\u4f8b\u3002<\/p>\n<p>\u7e3d\u800c\u8a00\u4e4b\uff0c\u5982\u679c\u4f60\u5728 Rails3 \u4e2d\u4e0d\u9700\u8981\u5168\u90e8\u7684 Controller \u7684\u529f\u80fd\uff0c\u60f3\u8981\u76e1\u91cf\u62c9\u9ad8\u6548\u80fd\uff0c\u6709\u5e7e\u7a2e\u63a8\u85a6\u4f5c\u6cd5\uff1a<\/p>\n<p>* \u5beb\u6210 Rack Middleware\uff0c\u7136\u5f8c\u5728 config\/application.rb \u4e2d\u63d2\u5165 <a href=\"http:\/\/config.middleware.us\" class=\"autohyperlink\">config.middleware.us<\/a>e YourMiddleWare<br \/>\n* \u5beb\u6210 Rack App\uff0c\u5728 config.route.rb \u4e2d\u5c07\u67d0\u500b\u5c0d\u61c9\u7684\u7db2\u5740\u6307\u5230\u9019\u500b Rack App<br \/>\n* \u7e7c\u627f\u81ea ActionController::Metal \u5be6\u4f5c\u4e00\u500b Controller\uff0c\u5176\u4e2d\u7684 action \u4e5f\u662f\u4e00\u500b Rack App<\/p>\n<p>\u5176\u4e2d\u7684\u5dee\u7570\u5c31\u5728\u65bc\u5f8c\u5169\u8005\u6703\u5728 Rails Route \u4e4b\u5f8c\uff08\u597d\u8655\u662f\u7d71\u4e00\u7531 route.rb \u7ba1\u7406 URL \u8def\u5f91\uff09\uff0c\u5982\u679c\u7e7c\u627f\u81ea ActionController::Metal \u53ef\u4ee5\u6709\u5f48\u6027\u7372\u5f97\u66f4\u591a Controller \u529f\u80fd\u3002\u539f\u5247\u4e0a\uff0c\u6211\u60f3\u6211\u6703\u63a8\u85a6 ActionController::Metal\uff0c\u5beb\u8d77\u4f86\u6700\u70ba\u7c21\u55ae\uff0c\u4e00\u81f4\u6027\u8ddf\u7dad\u8b77\u6027\u8f03\u9ad8\u3002<\/p>\n<p>\u53e6\u5916\uff0c\u9084\u6709\u500b\u5c0f\u73a9\u610f\uff0c <a href=\"http:\/\/www.engineyard.com\/blog\/2009\/rails-and-merb-merge-the-anniversary-part-1-of-6\/\">ActionController::Middleware<\/a> \u662f Controller \u5c64\u7d1a\u7684 Rack Middleware\uff0c\u8b93\u4f60\u53ef\u4ee5\u5728\u653e\u5165\u5230\u67d0\u500b\u7279\u5b9a Controller \u4e4b\u4e2d(\u4e5f\u5c31\u662f\u53ea\u6709\u8a72 Controller \u4f7f\u7528\u9019\u500b Middleware)\u3002\u4e0d\u904e\u5462\uff0c\u9019\u500b\u529f\u80fd\u6211\u5230\u73fe\u5728\u9084\u6c92\u770b\u5230\u4efb\u4f55\u5be6\u7528\u7684\u4f8b\u5b50\u5c31\u662f\u4e86\u3002 <\/p>\n<p>\u6700\u5f8c\uff0cYehuda \u63d0\u4f9b\u4e86\u4e00\u500b <a href=\"http:\/\/gist.github.com\/426004\">\u53c3\u8003\u6578\u64da<\/a>\uff1a<\/p>\n<pre>\r\n<code>\r\nfast: through middleware inserted at 0\r\nslwr: through middleware inserted via @use@\r\nrotr: through endpoint sent via the router\r\nbare: through ActionController::Metal with self.response_body\r\ncntr: through ActionController::Base with self.response_body\r\ntext: through ActionController::Base with render :text\r\ntmpl: through ActionController::Base with simple render :template\r\nlayt: through ActionController::Base with render :template with layout\r\n\r\n         real     rps\r\nfast   0.004271   2900 Rack \u6975\u9650\r\nslwr   0.067029   2200 \u4f7f\u7528 config.middleware.use YourMiddleware\r\nrotr   0.088085   2000 \u7d93\u904e Rails Route \u4e4b\u5f8c\r\nbare   0.103868   1900 \u4f7f\u7528 ActionController::Metal \u7684\u6700\u5feb\u901f\u5ea6\r\ncntr   0.355898   1070 \u4f7f\u7528 ActionController::Base \u7684\u6700\u5feb\u901f\u5ea6\r\ntext   0.557127    825 \u4f7f\u7528 ActionController::Base \u52a0\u4e0a render :text\r\ntmpl   0.639581    765 \u4f7f\u7528 ActionController::Base \u52a0\u4e0a render :template\r\nlayt   1.678789    375 \u4f7f\u7528 ActionController::Base \u52a0\u4e0a Template \u8ddf Layout\r\n<\/code>\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u65b0\u7684 Rails3 Controller \u91cd\u69cb\u5f8c\uff0c\u8b8a\u6210 ActionController::Base < Ac &hellip; \n\n<p class=\"link-more\"><a href=\"https:\/\/ihower.tw\/blog\/4561-rails3-metel\" class=\"more-link\">\u95b1\u8b80\u5168\u6587<span class=\"screen-reader-text\">\u3008Rails3: \u65b0\u7684 Metal \u6a5f\u5236\u3009<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[53],"tags":[],"class_list":["post-4561","post","type-post","status-publish","format-standard","hentry","category-rails","entry"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1q6tG-1bz","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/posts\/4561","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/comments?post=4561"}],"version-history":[{"count":24,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/posts\/4561\/revisions"}],"predecessor-version":[{"id":5516,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/posts\/4561\/revisions\/5516"}],"wp:attachment":[{"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/media?parent=4561"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/categories?post=4561"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/tags?post=4561"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}