Home arrow Ruby-on-Rails arrow Page 3 - Finishing a Basic Ruby-on-Rails Application

Finishing a Basic Ruby-on-Rails Application

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).

Author Info:
By: Apress Publishing
Rating: 5 stars5 stars5 stars5 stars5 stars / 3
February 14, 2011
  1. · Finishing a Basic Ruby-on-Rails Application
  2. · Adding Validations
  3. · Generating a Scaffold

print this article

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
    render :action => 'list'

  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
verify :method => :post, :only => [ :destroy, :create, :update ],
       :redirect_to => { :action => :list }

  def list
    @event_pages, @events = paginate :events, :per_page => 10

  def show
    @event = Event.find(params[:id])

  def new
    @event = Event.new

  def create
@event = Event.new(params[:event])
    if @event.save
      flash[:notice] = 'Event was successfully created.'
      redirect_to :action => 'list'
      render :action => 'new'

  def edit
    @event = Event.find(params[:id])

  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
      render :action => 'edit'

  def destroy
    redirect_to :action => 'list'

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.

blog comments powered by Disqus

- Ruby-on-Rails Faces Second Security Flaw in ...
- Ruby 2.0 Prepped for February 2013 Release
- Why LinkedIn Switched from Ruby on Rails
- Adding Style with Action Pack
- Handling HTML in Templates with Action Pack
- Filters, Controllers and Helpers in Action P...
- Action Pack and Controller Filters
- Action Pack Categories and Events
- Logging Out, Events and Templates with Actio...
- Action Pack Sessions and Architecture
- More on Action Pack Partial Templates
- Action Pack Partial Templates
- Displaying Error Messages with the Action Pa...
- Action Pack Request Parameters
- Creating an Action Pack Registration Form

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials