Early experiences with Active Resource

Early experiences with Active Resource

Revisions made by tamc2 up to 18:23 Fri 30 Jun 2006

Note, if you wish to edit or undo these revisions, please follow the links from here. You cannot do it from here, becuase someone may have edited the page more recently and because you may need a password to edit the page.

18:23 Fri 30 Jun 2006

  1. 52. You can then get the object, as expected:
  2. 52. You can then get an object:
  3. 58.
  4. 59. There is an error if you try and @find(:all)@ and no objects exists.

18:05 Fri 30 Jun 2006

  1. 91. [[ActiveResource::Base => http://dev.rubyonrails.org/browser/trunk/activeresource/lib/active_resource/base.rb#L54]]
  2. 91. [[ActiveResource::Connection => http://dev.rubyonrails.org/browser/trunk/activeresource/lib/active_resource/connection.rb#L54]]

18:04 Fri 30 Jun 2006

  1. 2. Last updated 22:45 BST Fri 30 June.
  2. 2. Last updated 23:04 BST Fri 30 June.
  3. 71.
  4. 72. I made this changes to get a create function:
  5. 73.
  6. 74. [[ActiveResource::Base => http://dev.rubyonrails.org/browser/trunk/activeresource/lib/active_resource/base.rb#L65]]
  7. 75. <pre>
  8. 76. <code>
  9. 77. def save
  10. 78. id ? update : create
  11. 79. end
  12. 80. </code>
  13. 81. </pre>
  14. 82.
  15. 83. <pre>
  16. 84. <code>
  17. 85. def create
  18. 86. attributes["id"] = connection.post(self.class.collection_path, to_xml)
  19. 87. end
  20. 88. </code>
  21. 89. </pre>
  22. 90.
  23. 91. [[ActiveResource::Base => http://dev.rubyonrails.org/browser/trunk/activeresource/lib/active_resource/base.rb#L54]]
  24. 92. <pre>
  25. 93. <code>
  26. 94. def post(path, body)
  27. 95. response = request(:post, path, body)
  28. 96. return response['Location'][/\/([^\/]*?)$/,1] # The id
  29. 97. end
  30. 98. </code>
  31. 99. </pre>

17:47 Fri 30 Jun 2006

  1. 2. Last updated 22:45 BST Fri 30 June
  2. 2. Last updated 22:45 BST Fri 30 June.

17:45 Fri 30 Jun 2006

  1. 2. Last updated 22:05 BST Fri 30 June
  2. 2. Last updated 22:45 BST Fri 30 June
  3. 59. h2. Put / Post / Update
  4. 59. h2. Put / Update
  5. 67.
  6. 68. h2. Post / Create
  7. 69.
  8. 70. Creating a record doesn't work, as @Person.save@ currently goes straight to the put method.

17:44 Fri 30 Jun 2006

  1. 0. content moved to [[]]
  2. 0. h1. Early experiences with Active Resource
  3. 1.
  4. 2. Last updated 22:05 BST Fri 30 June
  5. 3.
  6. 4. At the recent RailsConf David Heinemeier Hansson announced his ideas for an active resource library in [[rails => http://www.rubyonrails.com ]]. His slides are available [[ here => http://www.loudthinking.com/lt-files/worldofresources.pdf ]].
  7. 5.
  8. 6. Unfortunately the early version he checked into the [[ rails edge repository => http://dev.rubyonrails.org/browser/trunk/activeresource ]] doesn't quite match his slides.
  9. 7.
  10. 8. Below are my (in progress) notes on getting it to work.
  11. 9.
  12. 10. h2. Pre-requisites
  13. 11.
  14. 12. You need edge rails: @rake rails:freeze:edge@
  15. 13.
  16. 14. You need some rest-ful type routes. I've used: @script/plugin install simply_restful@
  17. 15.
  18. 16. You need a controller that has the same name as your model (e.g. PeopleController) with index, new, create, show, edit, update and delete methods. Each of these needs to respond sensibly to xml requests, e.g.:
  19. 17.
  20. 18. <pre>
  21. 19. <code>
  22. 20. def show
  23. 21. @patch = Patch.find(params[:id])
  24. 22. respond_to do |format|
  25. 23. format.html
  26. 24. format.xml { render :xml => @patch.to_xml }
  27. 25. end
  28. 26. end
  29. 27. </code>
  30. 28. </pre>
  31. 29.
  32. 30. h2. Initialization
  33. 31.
  34. 32. The example that DHH gave:
  35. 33. <pre>
  36. 34. <code>
  37. 35. Person = ActiveResource::Struct.new do |person|
  38. 36. person.uri = "http://api.myremote.com/people"
  39. 37. person.credentials :name => "me", :password => "password"
  40. 38. end
  41. 39. </code>
  42. 40. </pre>
  43. 41.
  44. 42. What works in the code:
  45. 43. <pre>
  46. 44. <code>
  47. 45. Person = ActiveResource::Struct.create
  48. 46. Person.site = "http://api.myremote.com/people"
  49. 47. </code>
  50. 48. </pre>
  51. 49.
  52. 50. h2. Get
  53. 51.
  54. 52. You can then get the object, as expected:
  55. 53. <pre>
  56. 54. <code>
  57. 55. p = Person.find 1
  58. 56. </code>
  59. 57. </pre>
  60. 58.
  61. 59. h2. Put / Post / Update
  62. 60.
  63. 61. Saving doesn't work, because ActiveResource doesn't set the content-type. Adding this at the start of the request method in [[ActiveResource::Connection => http://dev.rubyonrails.org/browser/trunk/activeresource/lib/active_resource/connection.rb#L60]] seems to help:
  64. 62. <pre>
  65. 63. <code>
  66. 64. arguments << { 'content-type' => 'application/xml' }
  67. 65. </code>
  68. 66. </pre>

17:44 Fri 30 Jun 2006

  1. 0. h1. Early experiences with Active Resource
  2. 1.
  3. 0. content moved to [[]]
  4. 2. Last updated 22:05 BST Fri 30 June
  5. 3.
  6. 4. At the recent RailsConf David Heinemeier Hansson announced his ideas for an active resource library in [[rails => http://www.rubyonrails.com ]]. His slides are available [[ here => http://www.loudthinking.com/lt-files/worldofresources.pdf ]].
  7. 5.
  8. 6. Unfortunately the early version he checked into the [[ rails edge repository => http://dev.rubyonrails.org/browser/trunk/activeresource ]] doesn't quite match his slides.
  9. 7.
  10. 8. Below are my (in progress) notes on getting it to work.
  11. 9.
  12. 10. h2. Pre-requisites
  13. 11.
  14. 12. You need edge rails: @rake rails:freeze:edge@
  15. 13.
  16. 14. You need some rest-ful type routes. I've used: @script/plugin install simply_restful@
  17. 15.
  18. 16. You need a controller that has the same name as your model (e.g. PeopleController) with index, new, create, show, edit, update and delete methods. Each of these needs to respond sensibly to xml requests, e.g.:
  19. 17.
  20. 18. <pre>
  21. 19. <code>
  22. 20. def show
  23. 21. @patch = Patch.find(params[:id])
  24. 22. respond_to do |format|
  25. 23. format.html
  26. 24. format.xml { render :xml => @patch.to_xml }
  27. 25. end
  28. 26. end
  29. 27. </code>
  30. 28. </pre>
  31. 29.
  32. 30. h2. Initialization
  33. 31.
  34. 32. The example that DHH gave:
  35. 33. <pre>
  36. 34. <code>
  37. 35. Person = ActiveResource::Struct.new do |person|
  38. 36. person.uri = "http://api.myremote.com/people"
  39. 37. person.credentials :name => "me", :password => "password"
  40. 38. end
  41. 39. </code>
  42. 40. </pre>
  43. 41.
  44. 42. What works in the code:
  45. 43. <pre>
  46. 44. <code>
  47. 45. Person = ActiveResource::Struct.create
  48. 46. Person.site = "http://api.myremote.com/people"
  49. 47. </code>
  50. 48. </pre>
  51. 49.
  52. 50. h2. Get
  53. 51.
  54. 52. You can then get the object, as expected:
  55. 53. <pre>
  56. 54. <code>
  57. 55. p = Person.find 1
  58. 56. </code>
  59. 57. </pre>
  60. 58.
  61. 59. h2. Put / Post / Update
  62. 60.
  63. 61. Saving doesn't work, because ActiveResource doesn't set the content-type. Adding this at the start of the request method in [[ActiveResource::Connection => http://dev.rubyonrails.org/browser/trunk/activeresource/lib/active_resource/connection.rb#L60]] seems to help:
  64. 62. <pre>
  65. 63. <code>
  66. 64. arguments << { 'content-type' => 'application/xml' }
  67. 65. </code>
  68. 66. </pre>

17:07 Fri 30 Jun 2006

  1. 2. Last updated 22:05 BST Fri 30 June
  2. 3.
  3. 9.
  4. 10. h2. Pre-requisites
  5. 11.
  6. 12. You need edge rails: @rake rails:freeze:edge@
  7. 13.
  8. 14. You need some rest-ful type routes. I've used: @script/plugin install simply_restful@
  9. 15.
  10. 16. You need a controller that has the same name as your model (e.g. PeopleController) with index, new, create, show, edit, update and delete methods. Each of these needs to respond sensibly to xml requests, e.g.:
  11. 17.
  12. 18. <pre>
  13. 19. <code>
  14. 20. def show
  15. 21. @patch = Patch.find(params[:id])
  16. 22. respond_to do |format|
  17. 23. format.html
  18. 24. format.xml { render :xml => @patch.to_xml }
  19. 25. end
  20. 26. end
  21. 27. </code>
  22. 28. </pre>

17:02 Fri 30 Jun 2006

  1. 39. Saving doesn't work, because ActiveResource doesn't set the content-type. Adding this at the start of the request method in [[ActiveResource::Connection => http://dev.rubyonrails.org/browser/trunk/activeresource/lib/active_resource/connection.rb]] seems to help:
  2. 39. Saving doesn't work, because ActiveResource doesn't set the content-type. Adding this at the start of the request method in [[ActiveResource::Connection => http://dev.rubyonrails.org/browser/trunk/activeresource/lib/active_resource/connection.rb#L60]] seems to help:

17:01 Fri 30 Jun 2006

  1. 28. h2. Get
  2. 29.
  3. 36.
  4. 37. h2. Put / Post / Update

17:01 Fri 30 Jun 2006

  1. 0. h1. Early experiences with Active Resource
  2. 1.
  3. 2. At the recent RailsConf David Heinemeier Hansson announced his ideas for an active resource library in [[rails => http://www.rubyonrails.com ]]. His slides are available [[ here => http://www.loudthinking.com/lt-files/worldofresources.pdf ]].
  4. 3.
  5. 4. Unfortunately the early version he checked into the [[ rails edge repository => http://dev.rubyonrails.org/browser/trunk/activeresource ]] doesn't quite match his slides.
  6. 5.
  7. 6. Below are my (in progress) notes on getting it to work.
  8. 7.
  9. 8. h2. Initialization
  10. 9.
  11. 10. The example that DHH gave:
  12. 11. <pre>
  13. 12. <code>
  14. 13. Person = ActiveResource::Struct.new do |person|
  15. 14. person.uri = "http://api.myremote.com/people"
  16. 15. person.credentials :name => "me", :password => "password"
  17. 16. end
  18. 17. </code>
  19. 18. </pre>
  20. 19.
  21. 20. What works in the code:
  22. 21. <pre>
  23. 22. <code>
  24. 23. Person = ActiveResource::Struct.create
  25. 24. Person.site = "http://api.myremote.com/people"
  26. 25. </code>
  27. 26. </pre>
  28. 27.
  29. 28. You can then get the object, as expected:
  30. 29. <pre>
  31. 30. <code>
  32. 31. p = Person.find 1
  33. 32. </code>
  34. 33. </pre>
  35. 34.
  36. 35. Saving doesn't work, because ActiveResource doesn't set the content-type. Adding this at the start of the request method in [[ActiveResource::Connection => http://dev.rubyonrails.org/browser/trunk/activeresource/lib/active_resource/connection.rb]] seems to help:
  37. 36. <pre>
  38. 37. <code>
  39. 38. arguments << { 'content-type' => 'application/xml' }
  40. 39. </code>
  41. 40. </pre>
View, Edit or see all changes to this page.