Quick guide to the new scaffold_resource generator 20
UPDATE: this article has been updated for Rails 2.0 here.
Before I begin, I want to thank Ezra for making me aware that this generator is now available in edge rails.
Recently I described how to live on the edge. And if you are, you can take advantage of the new scaffold_resource generator.
This generator is not yet complete, so some of what I'm describing will probably chanage before 1.2 is officially released.
Type:
c:\myproject> ruby script\generate
You'll get some general help, including a listing of all the available generators. If scaffold_resource is one of them, you're in luck. If not, just unfreeze your project, then re-freeze to get the latest and greatest edge.
The Hard Part
Let's add a new REST-compliant resource for our project called Products. By convention the controller will be called Products, the table name will be Products, and the model class will be Product.
c:\myproject> ruby script\generate scaffold_resource Product
You pass the model name, not the controller name, on the command line.
And you get a ton of stuff generated for you automatically. This is a great way to learn the whole REST/CRUD design that DHH has been talking about. Explore all the files that were created and see how the views call various controller actions. Notice the use of named routes and helpers.
One thing you have to do by hand: Add a map.resources line to your routes.rb file:
map.resources :products
Done. Whew, I'm exhausted. You've typed, what, two whole lines?
The Payoff
So what did you get for all your hard work?
- A controller named Products with all of the canonical CRUD actions defined for you with boilerplate code. Study this code. Make sure you understand what's going on. Notice the comments above each action, they remind you which routing paths (url pattern + http verb) invoke that action. And note all the cool xml REST stuff you get for free. Your resource already has an HTTP XML API defined for it.
- Boilerplate views for four of the CRUD actions (you'll just need to fill in your model's details in the forms). Why not seven views for all seven actions? Because some of them don't need their own views. Study the code.
- A migration script. Define your table here, then run rake db:migrate.
But wait, there's more.
- You get a starter set of functional tests for your controller. Keep your tests passing and you'll know your REST design is still intact.
Typing at your computer drives away FUD. So get started. Generate a sandbox app, freeze to edge rails, use the scaffold_generator for one model, and look at the code.




I thought the convention was for the table name to be lowercase.
Hi Peter. I don't think I mentioned anything about table names being lowercase. Model and controller names start with an uppercase letter (like Product in my example above) because those are Ruby class names. But let me know if I didn't understand your question.
doesnt seem to work for me
the /new form isnt picking up any of my fields
ok
my views had no code to show the fields
so i manually added the input fields for my new item form
Is that how its meant to be?
Not sure how to implment restful form validation either :\
Note for nubies: I was having problems using scaffold_resource until I figured out that I had put the map.resources line at the bottom of routes.rb instead of the top - routes have sequential priority.
If you want to generate the scaffold_resource with the table names on it already, simply do something like
script/generate scaffold_resource Product columnname:columntype
i.e. script/generate scaffold_resource Product sku:string model:string description:text active:boolean
If you need to add limits to fields etc, you can edit your migration before you run it to add these in.
This will then add those input fields automatically in your code and you can start CRUDing straight away. It's also easier to manipulate than the older scaffold generator.
The latest scaffold_resource generator in Rails 1.2 RC1 adds the map.resources line at the top of routes.rb for you.
The latest build seems to have replaced: script/generate scaffold_resource SomeModel with: script/generate resource SomeModel
If you look in the RoR boo (p410), add the column names in using the col:type syntax and you will get them in all of the scaffolding forms. Very cool, I think. Obviously you still have to put things like :null => false in yourself but I like it a lot.
Can anyone advise on using REST with a singular model item like a CART as in shopping cart. All the examples I've seen are for plural items like products, but I'd like to try out a model and controller for a single cart, which would then have nested resources of cart items.
I have a question probably related to Rob's. In this series I see that we have generated our model in the singular, and we generated our controller in the singular. But we get plural urls no matter whether the action is working on a single resource instance or many. Is that the REST way, or is there a way to use REST in the Rails way and also get singular urls?
Amazing, I had small app for learning REST which is pretty much basic rails app, then I tried resourcescaffold for same model as used in my app. Opened articlecontroller file to show how rails and REST work and it was almost EXACTLY the same as I wrote(i didn't realize it was the new one at that moment), I was like ...wait I didn't write this comment.
It appears as though this does not exist in the rails 2.0 preview release. Only script/generate resource which seems to do only half the job (no scaffolding). script/generate doesn't even create the skeleton for your controller, it's just a blank class!
<quote> It appears as though this does not exist in the rails 2.0 preview release. Only script/generate resource which seems to do only half the job (no scaffolding). script/generate doesn't even create the skeleton for your controller, it's just a blank class! </quote>
in rails 2.0 you use script/generate scaffold for a fully blown scaffold
the new one looks like it adds the mapping for you too :-) Thanks for the writeup i was cursing at rails for a while after the update. had to learn stuff all over again.
i think you need to revise the 'new' title somewhat.. :)
halo good afternoon to each and everyone... can somebody help me with this problem....
everytime i run my program in aptana radrails i always got this error and i don't know what does it mean.
halo good afternoon to each and everyone... can somebody help me with this problem....
everytime i run my program in aptana radrails i always got this error and i don't know what does it mean.
NameError in TagsController#index uninitialized constant TagsController
The things in themselves have lying before them, in natural theology, natural causes, as is evident upon close examination.
The objects in space and time, in respect of the intelligible character, are the mere results of the power of our a priori knowledge, a blind but indispensable function of the soul, as is evident upon close examination.