Last updated

New in Rails: Resource Scaffold Generator

Oh boy! Rails 1.2 is all about resources. A product entry in your application is not just a rendered HTML page, but it “is” data. Rails 1.2 allows you to add a .xml extension to your url to retrieve the same product information in XML format!

Now, this all sounds hard. Two ways of rendering the same action depending on an extension. Generating XML code for every model in your database. No way you want to spend time developing all that stuff.

Well, Rails wouldn’t be Rails if there weren’t a generator for it!

This example is really easy. I want to create products in my database and expose them as HTML and XML to my users. Just do the following:

1$ ./script/generate scaffold_resource Product title:string description:text

A migration is generated automatically so update that if you want to before running

1$ rake db:migrate

As you can see in config/routes.rb, Rails has automatically generated routes for this resource!

1map.resource :products

Now you can start up your Rails app and access http://localhost:8000/products. Add some products if you like. Now if you got to http://localhost:8000/products/1 you a nice HTML scaffold. If you add .xml: http://localhost:8000/products/1.xml you’ll get nice XML!

Well, writing a seperate desktop application that retrieves informatin from your web application just got 100x easier!