{"id":1541,"date":"2006-12-10T02:53:18","date_gmt":"2006-12-09T18:53:18","guid":{"rendered":"http:\/\/ihower.idv.tw\/blog\/archives\/1541"},"modified":"2015-09-05T10:49:59","modified_gmt":"2015-09-05T02:49:59","slug":"rails-restful-%e7%9b%b8%e9%97%9c%e5%b7%a5%e5%85%b7","status":"publish","type":"post","link":"https:\/\/ihower.tw\/blog\/1541-rails-restful-%e7%9b%b8%e9%97%9c%e5%b7%a5%e5%85%b7","title":{"rendered":"Rails RESTful \u76f8\u95dc\u5de5\u5177"},"content":{"rendered":"<p>Update(2008\/5): \u4e8b\u9694\u4e00\u5e74\u591a\uff0c\u6211\u5c0d\u65bc RESTful Rails \u6709\u5b8c\u5168\u4e0d\u540c\u7684\u5207\u5165\u9ede\uff0c\u8a73\u898b <a href=\"\/blog\/archives\/1720\">Practical Rails2<\/a> \u6295\u5f71\u7247\u3002\u672c\u7bc7\u7684 Resource Generator \u548c simply_helper \u7686\u5df2\u5167\u5efa\u5728 Rails2 \u4e2d\u3002<\/p>\n<p>Rails RESTful <a href=\"\/blog\/archives\/1542\"><font color=\"#166aca\">\u7cfb<\/font><\/a> <a href=\"\/blog\/archives\/1545\"><font color=\"#166aca\">\u5217<\/font><\/a> <a href=\"\/blog\/archives\/1543\">\u5f48<\/a>\u00a0\u7b2c\u56db\u7bc7\uff0c\u6211\u5728\u9019\u88e1\u505a\u4e9b Rails RESTful \u5de5\u5177\u4ecb\u7d39\uff0c\u76f8\u95dc\u6587\u7ae0\u6709 <a href=\"http:\/\/programmin.prim8.net\/archives\/26-REST-tools-for-Rails-1.2.html\">REST tools for Rails 1.2<\/a>\u3002<\/p>\n<p><strong>Resource Generator<\/strong><\/p>\n<p>Rails \u7684 Generator \u662f\u6700\u7acb\u5373\u7684\u5b78\u7fd2\u6559\u6750\uff0c\u56e0\u6b64 RESTful \u7248\u7684 generator \u4e5f\u6709\u4e86\uff0c\u9664\u4e86\u7522\u751f model, controller, views, tests \u4e4b\u5916\uff0c\u9023 migration \u4e5f\u6703\u7522\u751f\u3002<\/p>\n<blockquote><p>ruby script\/generate scaffold_resource person name:string age:integer created_at:datetime birthday:date<\/p><\/blockquote>\n<p><!--more--><a href=\"http:\/\/www.matthewman.net\/articles\/2006\/09\/04\/new-rails-feature-simply_helpful\"><strong>simply_helper<\/strong><\/a>\u00a0<\/p>\n<p>\u642d\u914d Rails 1.2 \u4f7f\u7528\u7684 helper plugin\uff0c\u6709 rendering collections\u3001\u642d\u914d RJS \u4f7f\u7528\u7684 autogenerate DOM ID \u4ee5\u53ca\u642d\u914d RESTful \u4f7f\u7528\u7684\u8d85\u7c21\u5316 form blocks \u7b49\u3002<\/p>\n<p>\u672c\u4f86\u7684 render :partial =&gt; &#8216;venue&#8217;, :collection =&gt; @venues \u53ef\u4ee5\u7528\u66f4\u7c21\u55ae\u7684\u7e2e\u5beb\u8a9e\u6cd5\uff0cRuby \u6703\u81ea\u52d5\u8ff4\u5708\u4ee3\u5165<font face=\"Courier New\"> venues\/_venue.rhtml\u00a0\u6a23\u677f<\/font>\u00a0:<\/p>\n<blockquote><p>render :partial =&gt; @venues<\/p><\/blockquote>\n<p>dom_id \u5c07\u6839\u64da\u50b3\u9032\u7684 object\u2019s class\u00a0\u8ddf database ID\u00a0\u7522\u751f\u4e00\u500b\u00a0HTML ID\uff0c\u4f8b\u5982 <code>dom_id(@person) \u5c07\u6703\u7522\u751f<\/code> <code>person_123\uff0c\u5982\u679c\u9084\u672a\u5b58\u9032\u8cc7\u6599\u5eab\uff0c\u5247\u6703\u7522\u751f<\/code> <code>new_<em>object_name\u3002<\/em><\/code><\/p>\n<blockquote><p>dom_id(object, prefix = nil)<\/p><\/blockquote>\n<p>\u9019\u500b\u5c0f\u73a9\u610f\u6709\u4ec0\u9ebc\u7528? \u9019\u4e3b\u8981\u7528\u5728 RJS \u4e0a\uff0c\u4ed6\u8b93\u4f60\u53ef\u4ee5\u65b9\u4fbf\u53c3\u7167\u5230 DOM objects\uff0c\u4f8b\u5982\u4ee5\u4e0b\u4e09\u884c\u662f\u7b49\u50f9\u7684:<\/p>\n<blockquote><p>page[:person_123]<br \/>\npage[dom_id(@person)]<br \/>\npage[@person]<\/p><\/blockquote>\n<p>\u53e6\u5916 dom_class(object) \u5247\u6703\u6839\u64da object&#8217;s class \u7522\u751f\u4e00\u500b CSS class name\uff0c\u4e0d\u904e\u7528\u8655\u4f3c\u4e4e\u4e0d\u5927\u5c31\u662f\u4e86\u3002<\/p>\n<blockquote><p>form_for @post do |f|<\/p><\/blockquote>\n<p>\u8d85\u7c21\u6f54\u7684\u8868\u55ae\u751f\u6210\uff0c\u65b0\u589e\u8ddf\u7de8\u8f2f\u901a\u7528\u3002\u5b83\u6703\u6839\u64da @post \u662f\u5426\u5df2\u7d93\u5b58\u5728\u8cc7\u6599\u5eab\u4f86\u6c7a\u5b9a action (POST\u6216PUT)\uff0c\u4e5f\u6703\u751f\u6210 DOM id \u8ddf css class name\u3002\u9019\u7528\u6cd5\u6bd4 RESTful generator \u7522\u751f\u7684\u7248\u672c\u7528\u6cd5\u66f4\u7c21\u6f54\uff0cgenerator \u7684\u539f\u59cb\u7248\u672c\u5982\u4e0b(\u7d50\u679c\u4e00\u6a23\uff0c\u9664\u4e86\u4e0d\u6703\u7522\u751fDOM id\u8ddfclass name):<\/p>\n<blockquote><p>#\u65b0\u589e<br \/>\nform_for(:post, :url =&gt; posts_path) do |f|<br \/>\n#\u7de8\u8f2f<br \/>\nform_for(:post, :url =&gt; post_path(@post), :html =&gt; { :method =&gt; :put }) do |f|<\/p><\/blockquote>\n<p><a href=\"http:\/\/www.agilewebdevelopment.com\/plugins\/restful_authentication\"><font color=\"#166aca\"><strong>Restful Authentication Plugin<\/strong><\/font><\/a><\/p>\n<p>RESTful \u7248\u7684\u7db2\u7ad9\u767b\u5165\u529f\u80fd\uff0c\u5b83\u628a users \u8ddf session \u7576\u6210\u5169\u500b Resource\u3002\u9664\u4e86\u7528\u4e00\u822c\u767b\u5165\u6a5f\u5236(\u7528\u700f\u89bd\u5668\u586b\u5beb\u767b\u5165\u8868\u55ae\uff0c\u7136\u5f8c\u7528Session\u5224\u65b7\u662f\u5426\u767b\u5165\u4e86) \u4e4b\u5916\uff0c\u4e5f\u52a0\u5165 HTTP Basic Authentication \u8a8d\u8b49\u6a5f\u5236 (\u82e5HTTP Request \u7684 header \u4e2d\u6709 username \u8ddfpassword\u7684\u8a71\uff0c\u6703\u7b97\u767b\u5165\u72c0\u614b)\u3002<\/p>\n<p>\u6839\u64daRESTful\u7684\u7406\u5ff5\uff0c\u61c9\u8a72\u662f\u5b8c\u5168\u7528 HTTP Authentication \u624d\u662f\uff0c\u4e0d\u904e\u6211\u8a8d\u70ba\u63d0\u4f9b HTTP Authentication \u6700\u91cd\u8981\u529f\u80fd\u662f Web API \u53ef\u4ee5\u6709\u8a8d\u8b49\uff0c\u8b93\u5176\u4ed6 machine client (\u975e\u4f7f\u7528\u8005\u700f\u89bd\u5668)\u00a0\u53ea\u8981\u5728 HTTP Request \u7684 header \u4e0a\u52a0\u4e0a username \u8ddf password \u5c31\u53ef\u8a8d\u8b49\u5b58\u53d6\u8cc7\u6e90\u3002\u81f3\u65bc\u964d\u4f4e Server \u8ca0\u64d4\u7684\u512a\u9ede\u4f3c\u4e4e\u4e0d\u662f\u5f88\u660e\u986f(\u53cd\u6b63\u5927\u5bb6\u4e5f\u90fd\u7528session\u9019\u9ebc\u4e45\u4e86)\u3002\u6240\u4ee5\u70ba\u4e86\u7db2\u7ad9\u7684 usability \u8ddf\u6709\u6f02\u4eae\u7684HTML\u767b\u5165\u8868\u55ae\uff0c\u8981\u7d66\u700f\u89bd\u5668\u4f7f\u7528\u8005\u767b\u5165\u5c31\u4e0d\u559c\u6b61\u7528 HTTP \u8a8d\u8b49\u4e86 (\u9019\u4e00\u5b9a\u6703\u8df3\u700f\u89bd\u5668\u81ea\u5df1\u7684\u8a8d\u8b49\u8996\u7a97\u5427?)<\/p>\n<p>By the way\uff0c\u7db2\u9801\u8981\u7528 HTTP Authentication (\u6703\u7522\u751f\u700f\u89bd\u5668\u8a8d\u8b49\u8996\u7a97) \u7684\u8a71\u6709\u5b98\u65b9 plugin\uff0c\u8acb\u898b <a href=\"http:\/\/www.ryandaigle.com\/articles\/2006\/12\/04\/whats-new-in-edge-rails-new-http-authentication-plugin-and-a-plea-to-contribute\">What&#8217;s New in Edge Rails: New Http Authentication Plugin &#8211; And a Plea to Contribute<\/a>\u3002<\/p>\n<p><strong>ActiveResource<\/strong><\/p>\n<p>\u4e0a\u4e00\u96c6\u958b\u5834\u7684 DHH \u6295\u5f71\u7247\u00a0<a href=\"http:\/\/www.loudthinking.com\/lt-files\/worldofresources.pdf\">Discovering a world of Resources on Rails<\/a>\u00a0\u6700\u5f8c\u63d0\u7684\u58d3\u8ef8\u61c9\u7528\uff0c\u7528\u4f86\u5b58\u53d6 REST Resource\uff0c\u9054\u5230 RESTful \u7684\u6975\u81f4\u767c\u63ee\u3002\u5b83\u63d0\u4f9b RESTful \u7cfb\u7d71\u4e2d client side \u7684\u529f\u80fd\uff0c\u5c07\u9060\u7aef\u7684 REST services \u4ee3\u7406\u6210\u672c\u5730\u7684 model object\u3002\u672c\u4f86\u8981\u5c0d\u672c\u5730\u8cc7\u6599\u5eab\u64cd\u4f5c\u7684 CRUD \u6539\u6210\u5c0d\u9060\u7aef\u7528 POST\/GET\/PUT\/DELETE\uff0c\u66f4\u591a\u53ef\u4ee5\u53c3\u8003\u00a0<a href=\"http:\/\/www.ryandaigle.com\/articles\/2006\/06\/30\/whats-new-in-edge-rails-activeresource-is-here\">What&#8217;s New in Edge Rails: The Ins and Outs of ActiveResource<\/a>\u00a0\u9019\u7bc7\u3002\u53ef\u60dc\u7684\u662f\u9019\u6771\u897f\u9084\u8d95\u4e0d\u53ca\u52a0\u5165 Rails 1.2\uff0c\u770b\u8d77\u4f86\u9084\u5f97\u7b49\u4e00\u9663\u5b50\uff0c\u6211\u8a8d\u70ba\u9019\u5c07\u662f Rails RESTful \u7684\u6bba\u624b\u7d1a\u61c9\u7528\u3002<\/p>\n<p>Update(2006\/12\/13): \u52a0\u5beb\u4e00\u7bc7 <a href=\"\/blog\/archives\/1565\">Rails RESTful ActiveResource \u5148\u7779\u70ba\u5feb<\/a>\u00a0\u53ef\u4ee5\u770b\u770b\u5be6\u969b\u7528\u6cd5\u3002<\/p>\n<p><strong><a href=\"http:\/\/beast.caboo.se\/\">Beast Forum<\/a><\/strong><\/p>\n<p>\u4e00\u500b\u7528 RESTful \u958b\u767c\u7684\u8a0e\u8ad6\u5340\u8edf\u9ad4\uff0c<a href=\"http:\/\/svn.techno-weenie.net\/projects\/beast\/\">source code<\/a>\u00a0\u6211\u60f3\u4e5f\u8a31\u53ef\u4ee5\u53c3\u8003\u770b\u770b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Update(2008\/5): \u4e8b\u9694\u4e00\u5e74\u591a\uff0c\u6211\u5c0d\u65bc RESTful Rails \u6709\u5b8c\u5168\u4e0d\u540c\u7684\u5207\u5165\u9ede\uff0c\u8a73\u898b Pr &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/ihower.tw\/blog\/1541-rails-restful-%e7%9b%b8%e9%97%9c%e5%b7%a5%e5%85%b7\" class=\"more-link\">\u95b1\u8b80\u5168\u6587<span class=\"screen-reader-text\">\u3008Rails RESTful \u76f8\u95dc\u5de5\u5177\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":[5,53,28],"tags":[],"class_list":["post-1541","post","type-post","status-publish","format-standard","hentry","category-programming","category-rails","category-rest","entry"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1q6tG-oR","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/posts\/1541","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=1541"}],"version-history":[{"count":4,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/posts\/1541\/revisions"}],"predecessor-version":[{"id":8288,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/posts\/1541\/revisions\/8288"}],"wp:attachment":[{"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/media?parent=1541"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/categories?post=1541"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/tags?post=1541"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}