In this conclusion to a four-part article series on building a basic Ruby-on-Rails application, you'll learn how to add validations, build the basic scaffolding, and more. This article is excerpted from chapter three of the book Beginning Rails: From Novice to Professional, written by Jeffery Allan Hardy, Cloves Carneiro Jr. and Hampton Catlin (Apress; ISBN: 1590596862).
Finishing a Basic Ruby-on-Rails Application - Generating a Scaffold (Page 3 of 3 )
You generate scaffolding using the scaffold generator. Youíre probably getting pretty used to generators by now. Rails makes heavy use of them because they help automate repetitive tasks and generally remove the chances for errors when creating new files. Unlike us, the generator wonít ever forget exactly how to name a file, nor will it make a typo when creating a class. Letís use the scaffold generator now and solve the mystery of how all this works.
$ ./script/generate scaffold Event
Since youíve already generated a few of these files, Rails will prompt you before it tries to overwrite any that already exist. Specifically, it will warn about theeventscontroller and the events functional test. Since all we added to theeventscontroller was thescaffolddeclaration, itís safe to overwrite it. The same goes for the test, so go ahead and answer Y when youíre asked.
With that out of the way, letís take a look at theeventscontroller again. As you can see in Listing 3-6, our controller is now chock-full of actions. Thereís one for each ofindex,list,show,new,create,edit,update, anddestroyóthe basic CRUD actions.
Listing 3-6. The app/controllers/events_controller.rb File
class EventsController < ApplicationController def index list render :action => 'list' end
def list @event_pages, @events = paginate :events, :per_page => 10 end
def show @event = Event.find(params[:id]) end
def new @event = Event.new end
def create @event = Event.new(params[:event]) if @event.save flash[:notice] = 'Event was successfully created.' redirect_to :action => 'list' else render :action => 'new' end end
def edit @event = Event.find(params[:id]) end
def update @event = Event.find(params[:id]) if @event.update_attributes(params[:event]) flash[:notice] = 'Event was successfully updated.' redirect_to :action => 'show', :id => @event else render :action => 'edit' end end
def destroy Event.find(params[:id]).destroy redirect_to :action => 'list' end end
If you refresh the application in your browser, youíll notice that nothing has changed. The generated scaffold is identical in every way to the dynamic type. The big difference, though, is that once youíve generated scaffolding, if you change your model, youíll have to regenerate it if you want your application to follow suit. Most of the time, however, youíll make the changes by hand.
Itís important that you realize why scaffolding exists and that youíre aware of its limitations. As weíve just demonstrated, scaffolding helps when you need to get something running quickly to test your assumptions. It wonít take you very far in the real world, and eventually youíll end up replacing most (if not all) of it.
Explore the generated code and see if you can figure out how it hangs together. Donít worry if you canít understand all of it. Weíll discuss it in depth in the chapters that follow. With everything you know about Rails already, you should be able to piece together most of it.
Try changing a few things to see what happens. If you inadvertently break something, you can always run the scaffolding generator again to revert to the original. Can you see how the views inapp/views/eventsare related to the actions? What about the response messages, likeEvent was successfully created? What happens when you change them? See if you can find where the error messages for failed validations are rendered. If you remove the message, does the record still get saved? Youíll learn a lot by exploring, so take as much time as you need.
We started this chapter by outlining the basics of our sample application. Then we rolled up our sleeves and created our database and configuration files. Based on the goals of our application, we began by creating the tables necessary to run the core of our Eventmodel and got a first look at the simplicity and flexibility that migrations give our development processes. Then the scaffolding allowed us to test our assumptions about the model and table we created by getting a firsthand look at it in action. Also, we took a first crack at adding in validations that ensure that we maintain the integrity of our data. In the chapters that follow, we will investigate each of these concepts in depth.
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.