{"id":1565,"date":"2006-12-13T21:39:51","date_gmt":"2006-12-13T13:39:51","guid":{"rendered":"http:\/\/ihower.idv.tw\/blog\/archives\/1565"},"modified":"2015-09-05T10:50:14","modified_gmt":"2015-09-05T02:50:14","slug":"rails-restful-activeresource-%e9%a0%90%e8%a6%bd","status":"publish","type":"post","link":"https:\/\/ihower.tw\/blog\/1565-rails-restful-activeresource-%e9%a0%90%e8%a6%bd","title":{"rendered":"Rails RESTful ActiveResource \u5148\u7779\u70ba\u5feb"},"content":{"rendered":"<p>\u5728\u4e0a\u4e00\u7bc7 <a href=\"\/blog\/archives\/1541\">Rails RESTful \u76f8\u95dc\u5de5\u5177<\/a> \u4e2d\u6709\u63d0\u5230\uff0cActiveReource \u662f\u4e00\u500b\u91dd\u5c0d Rails RESTful APIs \u6240\u7528\u7684 client-side <span class=\"caps\">XML<\/span> consumer\uff0c\u6211\u5011\u82b1\u4e86\u597d\u5e7e\u7bc7\u5b78\u7fd2\u70ba\u4f55 RESTful \u8ddf\u5982\u4f55\u7528 map.resources \u4f86\u67b6\u51fa server-side provider\uff0c\u800c\u5177\u9ad4\u7684\u91cd\u5927\u597d\u8655\u5247\u6703\u5728 ARes \u4e2d\u9ad4\u73fe\u51fa\u4f86\uff0c\u6211\u5011\u53ef\u4ee5\u50cf\u5c0d ActiveRecord \u7684\u7269\u4ef6\u64cd\u4f5c\u4e00\u6a23\u4f86\u8655\u7406 ARes\uff0c\u53ea\u662f\u5b83\u7684\u5167\u90e8\u904b\u4f5c\u5c0d\u8c61\u8b8a\u6210 Resource \u800c\u4e0d\u662f Database (\u4f60\u611f\u89ba\u4e0d\u5230\u4f60\u5728\u7528Web APIs :p)\u3002<\/p>\n<p>\u8b93\u6211\u5011\u4f86\u8a66\u73a9\u770b\u770b\u5427! \u56e0\u70ba\u4e0d\u53ea\u9023 Rails 1.2 \u9084\u6c92 (\u6700\u5f8c\u95dc\u982d<a href=\"http:\/\/dev.rubyonrails.org\/changeset\/5554\">\u62c9\u51fa<\/a> :p)\uff0c\u9023 Edge Rails \u7248\u90fd\u9084\u6c92\u6709\uff0c\u6240\u4ee5\u5fc5\u9808\u76f4\u63a5\u7528 svn co<\/p>\n<blockquote><p>svn co <a href=\"http:\/\/dev.rubyonrails.org\/svn\/rails\/trunk\" class=\"autohyperlink\">dev.rubyonrails.org\/svn\/rails\/trunk<\/a> vendor\/rails<\/p><\/blockquote>\n<p>\u6211\u5011\u7528 irb \u4f86\u958b\u59cb\u5be6\u9a57\u5427~<\/p>\n<blockquote><p>$ irb<br \/>\n> require &#8216;vendor\/rails\/activesupport\/lib\/active_support&#8217;<br \/>\n> require &#8216;vendor\/rails\/activeresource\/lib\/active_resource&#8217;<\/p><\/blockquote>\n<p><a href=\"\/blog\/archives\/1541\">\u4e0a\u56de<\/a>\u4e5f\u63d0\u5230 beast \u662f\u500b\u7528 RESTful \u958b\u767c\u7684\u8a0e\u8ad6\u5340\u8edf\u9ad4\uff0c\u6240\u4ee5\u6211\u5011\u7528 beast \u4f86\u505a\u64cd\u4f5c\u5c0d\u8c61:<!--more--><\/p>\n<blockquote><p>class User &lt; BeastResource<br \/>\nend<\/p>\n<p>class Forum &lt; BeastResource<br \/>\nend<\/p>\n<p>class Topic &lt; BeastResource<br \/>\nsite &lt;&lt; &#8216;\/forums\/:forum_id&#8217;<br \/>\nend<\/p>\n<p>class Post &lt; BeastResource<br \/>\nsite &lt;&lt; &#8216;\/forums\/:forum_id\/topics\/:topic_id&#8217;<br \/>\nend<\/p><\/blockquote>\n<p>\u81f3\u6b64 Web APIs \u5c31\u929c\u63a5\u8d77\u4f86\u4e86\uff0c\u975e\u5e38\u7c21\u55ae\u3002\u63a5\u4e0b\u4f86\u5c31\u662f\u795e\u5947\u7684\u5730\u65b9\u4e86\uff0c\u6211\u5011\u4f7f\u7528\u7684\u65b9\u5f0f\u5c31\u50cf\u64cd\u4f5c ActiveRecord \u4e00\u6a23:<\/p>\n<p>\u8b80\u53d6\u6587\u7ae0:<\/p>\n<blockquote><\/blockquote>\n<blockquote><p>f = Forum.find 1<br \/>\n# notice that since Topic has a prefix, we must pass the forum_id.<br \/>\n# This is so it can make the request to \/forums\/1\/topics\/1.xml<br \/>\nt = Topic.find 1, :forum_id =&gt; f.id<br \/>\np = Post.find 1, :forum_id =&gt; f.id, :topic_id =&gt; t.id<br \/>\nu = User.find <a href=\"http:\/\/p.us\" class=\"autohyperlink\">p.us<\/a>er_id<\/p><\/blockquote>\n<p>\u751a\u81f3\u662f\u5f35\u8cbc\u6587\u7ae0:<\/p>\n<blockquote><p>forums = Forum.find :all<br \/>\ntesting = forums.detect { |f| <a href=\"http:\/\/f.name\" class=\"autohyperlink\">f.name<\/a> == &#8216;Testing&#8217; }<br \/>\n# initialize takes two parameters in ActiveResource. One for the model parameters, and one for the prefix parameters.<br \/>\ntopic = Topic.new({<br \/>\n:title =&gt; &#8216;Testing out ARes&#8217;,<br \/>\n:body =&gt; &#8216;Testing 1, 2, 3!&#8217;},<br \/>\n{ :forum_id =&gt; testing.id })<\/p><\/blockquote>\n<p>\u958b\u59cb\u9a5a\u5606 RESTful \u7684\u5a01\u529b\u4e86\u55ce? \u5b83\u5c07 Rails action \u7684 create,find,update,destroy \u5c0d\u61c9\u5230 HTTP method \u7684 POST,GET,PUT,DELETE\uff0c\u7576\u7136\u6700\u5f8c\u53c8\u8b8a\u6210 SQL \u7684\u00a0INSERT,SELECT,UPDATE,DELETE\uff0c\u9019\u5c31\u662f Resources on Rails \u7684 CRUD \u54f2\u5b78\u3002<\/p>\n<table border=\"1\">\n<tr>\n<th>Model request<\/th>\n<th>Http <span class=\"caps\">REST<\/span> operation<\/th>\n<th>request body<\/th>\n<th>Request <span class=\"caps\">URI<\/span><\/th>\n<th>Response<\/th>\n<\/tr>\n<tr>\n<td>find(<em>id<\/em>)<\/td>\n<td><code>GET<\/code><\/td>\n<td><em>N\/A<\/em><\/td>\n<td>\/people\/<em>id<\/em>.xml<\/td>\n<td>\n<pre><code>   name <\/code><\/pre>\n<\/td>\n<\/tr>\n<tr>\n<td>save (update)<\/td>\n<td><code>PUT<\/code><\/td>\n<td>\n<pre><code>   name <\/code><\/pre>\n<\/td>\n<td>\/people\/<em>id<\/em>.xml<\/td>\n<td><code>Status: 200 OK<\/code><\/td>\n<\/tr>\n<tr>\n<td>save (create)<\/td>\n<td><code>POST<\/code><\/td>\n<td>\n<pre><code>   name <\/code><\/pre>\n<\/td>\n<td>\/people<\/td>\n<td><code>Location: http:\/\/x\/people\/id.xml<\/code><\/td>\n<\/tr>\n<tr>\n<td>destroy<\/td>\n<td><code>DELETE<\/code><\/td>\n<td><em>N\/A<\/em><\/td>\n<td>\/people\/<em>id<\/em>.xml<\/td>\n<td><code>Status: 200 OK<\/code><\/td>\n<\/tr>\n<tr>\n<td>list<\/td>\n<td><code>GET<\/code><\/td>\n<td><em>N\/A<\/em><\/td>\n<td>\/people<\/td>\n<td>\n<pre><code>        name    <\/code><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<p>\u5176\u4e2d\u50b3\u8f38\u683c\u5f0f\u4f7f\u7528XML\uff0c\u4e0d\u904e\u4f60\u4e5f\u4e0d\u7528\u8cbb\u5fc3\u6216\u770b\u5230XML\uff0c\u56e0\u70ba ARes \u4e5f\u90fd\u8655\u7406\u597d\u4e86\u3002\u800c\u5728 server-side \u90e8\u5206\uff0cRails \u7576\u7136\u4e5f\u4e0d\u7528\u4f60\u624b\u52d5\u751f\u6210XML\uff0c\u65b0\u7248 model \u90fd\u53ef\u4ee5\u7528 .to_xml \u81ea\u52d5\u751f\u6210 XML \u683c\u5f0f\uff0c\u6240\u4ee5\u53ea\u9700\u5728 Controller \u4e2d\u5beb\u597d\u5373\u53ef\uff0c\u4f8b\u5982:<\/p>\n<blockquote><p>def who_bought<br \/>\n@product = Product.find(params[:id])<br \/>\n@orders = @product.orders<br \/>\n<strong>respond_to do |accepts|<br \/>\n\u00a0accepts.html<br \/>\n\u00a0accepts.xml { render :xml =&gt; @product.to_xml(:include =&gt; :orders) }<br \/>\nend<br \/>\n<\/strong>end<\/p><\/blockquote>\n<p>Rails 1.2 \u9084\u53ea\u662f\u6253\u958b REStful \u6642\u4ee3\u7684\u5e8f\u5e55\uff0c\u975e\u5e38\u671f\u5f85 ActiveResource \u7684\u6b63\u5f0f\u63a8\u51fa\uff0c\u4ee5\u5f8c\u6211\u6703\u6301\u7e8c\u95dc\u6ce8 ActiveResource \u958b\u767c\u52d5\u614b\u3002<\/p>\n<p>\u53c3\u8003\u8cc7\u6599:<\/p>\n<ul>\n<li><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><\/li>\n<li><a href=\"http:\/\/weblog.techno-weenie.net\/2006\/12\/13\/taking-ares-out-for-a-test-drive\">Taking ARes Out For a Test Drive<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u4e0a\u4e00\u7bc7 Rails RESTful \u76f8\u95dc\u5de5\u5177 \u4e2d\u6709\u63d0\u5230\uff0cActiveReource \u662f\u4e00\u500b\u91dd\u5c0d Rails  &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/ihower.tw\/blog\/1565-rails-restful-activeresource-%e9%a0%90%e8%a6%bd\" class=\"more-link\">\u95b1\u8b80\u5168\u6587<span class=\"screen-reader-text\">\u3008Rails RESTful ActiveResource \u5148\u7779\u70ba\u5feb\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-1565","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-pf","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/posts\/1565","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=1565"}],"version-history":[{"count":1,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/posts\/1565\/revisions"}],"predecessor-version":[{"id":8289,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/posts\/1565\/revisions\/8289"}],"wp:attachment":[{"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/media?parent=1565"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/categories?post=1565"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/tags?post=1565"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}