Hi.
My question: How can I use Rails-Client to consume scaffold-generated web
services on Heroku?

I created a very simple REST application on Heroku (
restnotes.herokugarden.com)


script/generate scaffold note body:text priority:integer
rake db:migrate

The application is accessible through

http://restnotes.herokugarden.com/notes

I am able to create notes through
http://restnotes.herokugarden.com/notes/new


and the form-generated HTML looks like this:


<form action="/notes" class="new_note" id="new_note" method="post"><div
style="margin:0;padding:0"><input name="authenticity_token" type="hidden"
value="92c28cfca4a291126c4d5622611e9132a0d3e0d5" /></div>


  <p>
    <label for="note_body">Body</label><br />
    <textarea cols="40" id="note_body" name="note[body]"
rows="20"></textarea>
  </p>
  <p>
    <label for="note_priority">Priority</label><br />
    <input id="note_priority" name="note[priority]" size="30" type="text"
/>
  </p>
  <p>
    <input id="note_submit" name="commit" type="submit" value="Create" />
  </p>
</form>



Now I would like to use a rest-client 0.9 to interact with my  notes
application.

C:\Users\Jay>irb
irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'rest_client'
=> true
irb(main):003:0> RestClient.get 'restnotes.herokugarden.com/notes'
=> "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<notes type=\"array\">\n
 <note>\n    <body>This is a pretty cool n
ew note</body>\n    <created-at
type=\"datetime\">2009-02-26T19:14:03Z</created-at>\n    <id
type=\"integer\">1</id
>\n    <priority type=\"integer\">1</priority>\n    <updated-at
type=\"datetime\">2009-02-26T19:14:03Z</updated-at>
\n  </note>\n</notes>\n"
irb(main):004:0>

So far so good...This is the same note I created with the form.

irb(main):015:0* RestClient.post 'restnotes.herokugarden.com/notes',
:body=>'test note', :priority => '1'
RestClient::RequestFailed: HTTP status code 422
        from
C:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/rest-client-0.9/lib/restclient/request.rb:136:in
`process_result'
        from
C:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/rest-client-0.9/lib/restclient/request.rb:99:in
`transmit'
        from C:/InstantRails-2.0-win/ruby/lib/ruby/1.8/net/http.rb:547:in
`start'
        from
C:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/rest-client-0.9/lib/restclient/request.rb:95:in
`transmit'
        from
C:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/rest-client-0.9/lib/restclient/request.rb:34:in
`execute_inner'
        from
C:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/rest-client-0.9/lib/restclient/request.rb:26:in
`execute'
        from
C:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/rest-client-0.9/lib/restclient/request.rb:12:in
`execute'
        from
C:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/rest-client-0.9/lib/restclient.rb:57:in
`post'
        from (irb):15
irb(main):016:0>

Posting with formatted parameters didn't work either.
irb(main):021:0> RestClient.post 'restnotes.herokugarden.com/notes',
"note[body]"=>"TestNOTE", "note[priority]" => "3"


I created another note and then I used Rest Client to get and delete a note
and it worked:

irb(main):019:0* RestClient.get 'http://restnotes.herokugarden.com/notes/2'
=> "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<note>\n
 <body>test</body>\n  <created-at type=\"datetime\">2009-0
2-26T19:32:27Z</created-at>\n  <id type=\"integer\">2</id>\n  <priority
type=\"integer\">2</priority>\n  <updated-a
t type=\"datetime\">2009-02-26T19:32:27Z</updated-at>\n</note>\n"
irb(main):020:0> RestClient.delete '
http://restnotes.herokugarden.com/notes/2'
=> " "
irb(main):021:0>



At this point, I decided to try building REST client with ActiveResource.


C:\Users\Jay>irb
irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'ActiveResource'
=> true
irb(main):003:0> class Note < ActiveResource::Base
irb(main):004:1> self.site='http://restnotes.herokugarden.com'
irb(main):005:1> end
=> "http://restnotes.herokugarden.com";
irb(main):006:0>
irb(main):007:0*
irb(main):008:0*

I was able to use it to find existing notes:

irb(main):009:0* Note.find :all
=> [#<Note:0x5a5d1b0 @attributes={"updated_at"=>Thu Feb 26 19:35:57 UTC
2009, "body"=>"test", "priority"=>3, "id"=>
3, "created_at"=>Thu Feb 26 19:35:57 UTC 2009}, @prefix_options={}>]

I then used it to successfully create a new note with POST (through
ActiveResource).

irb(main):010:0> new_note=Note.new(:body=>"ActiveResource client test note",
:priority =>"5")
=> #<Note:0x5a57d3c @attributes={"body"=>"ActiveResource client test note",
"priority"=>"5"}, @prefix_options={}>
irb(main):011:0> new_note.save
=> true

A look at the web interface showed that the new note was created. Then I
modified it:

irb(main):012:0> new_note.body="There are monkeys on mars"
=> "There are monkeys on mars"
irb(main):013:0> new_note.priority="99"
=> "99"
irb(main):014:0> new_note.save
=> true
irb(main):015:0>

The PUT worked as well to change the values of the note.

At this point, I'm at a loss because I have no idea how to use Rest-client
to POST new notes or to change (PUT) the fields for this scaffold-generated
resource. Can anybody help?


Thanks,   Jay

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Heroku" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/heroku?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to