@madpilot makes

Creating web services in Ruby on Rails

I’ve been working on setting up web services in Ruby on Rails for 88 Miles and have discovered somethings that may be useful to other users. It may save someone some time, as the documentation for this particular area is a little sparse.

1. Putting a colon(:) in front of a return type indicates a primative – i.e an integer or string. If you want to return a complex type, leave off the colon

  1. api_method :find, :expects => [ { :id => :int }, { :user_name => :string }, { :password => :string } ], :returns => [ Shift ]

This will return the “Shift” Object. You can return Arrays by using two square brackets:

  1. api_method :find_all, :expects => [ { :project_id => :int }, { :user_name => :string }, { :password => :string } ], :returns => [[ Shift ]]

This will return an array of “Shift” objects.

2. Raising an exception with only a message invokes a SOAPException (and probably a XML-RPC error too – haven’t tried that yet), which is a much better way of handling errors than returning integers or strings (For those playing at home, check out the “fault” body of the SOAP message)

  1. raise Exception, “This is the string that gets returned”

3. If you are trying to access the webservices via WDSL, simply add /wsdl at the end of the URL

http://yoursite/controller/wdsl

4. You can generate the scaffolds for a webservice using the generate script:

# script/generate web_service controller_name

5 comments

  1. Will you please guide me of any complete tutorial on accessing and implementation of web services in Ruby on rails.. iam a new bie and similarly

    i wanna know how to implement internationalization support on RoR
  2. Hi Sathya,



    I have some info some where. I'll dig it up and let you know.
  3. If you ever find a pointer to that info I would love to look it over as well.

    Seems that I have a daffy customer with a lot of already written code in a .Net framework :-((
  4. Hi! I was working on one project using rails where I had to expose some methods as web service. But the method was returning an array. Your post helped me in creating a service that can return an array. Thanks a lot.
  5. No worries! Glad you found it useful.

Leave a comment