Home arrow Ruby-on-Rails arrow Page 2 - Database Tables, Controllers for Ruby-on-Rails Applications
RUBY-ON-RAILS

Database Tables, Controllers for Ruby-on-Rails Applications


In this third part of a four-part article series on building a basic Ruby-on-Rails application, you will learn how to create a database table, generate a controller, 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 / 2
February 09, 2011
TABLE OF CONTENTS:
  1. · Database Tables, Controllers for Ruby-on-Rails Applications
  2. · Generating a Controller

print this article
SEARCH DEVARTICLES

Database Tables, Controllers for Ruby-on-Rails Applications - Generating a Controller
(Page 2 of 2 )

We’ve created a model and its supporting database table, so our next step is to work on the controller and view side of the application. We’ll create a controller named (wait for it) eventsto control the operation of the events functionality of the application. Just as with models, Rails provides a generator that we can use to create controllers. Let’s start using it.

$ ./script/generate controller events

--------------------------------------------
exists  app/controllers/
exists  app/helpers/
create  app/views/events
create  test/functional/
create  app/controllers/events_controller.rb
reate test/functional/events_controller_test.rb create  app/helpers/events_helper.rb
--------------------------------------------

The controller generator created three files:

  1. app/controllers/events_controller.rb: The controller that will be responsible for handling requests and responses for anything to do with events.
  2. test/functional/events_controller_test.rb: The class that will contain all functional tests for theeventscontroller (we’ll cover testing applications in Chapter 8). 
     
  3. app/helpers/events_helper.rb: Thehelper class in which you can add utility methods that can be used in your views.

The controller generator also created an empty directory inapp/viewscalledevents. This is where we’ll place the templates for theeventscontroller.

Up and Running with Scaffolding

One of the killer features that gave a lot of exposure to Rails is its scaffolding capabilities. Scaffolding allows you to create a boilerplate-style set of actions and templates that make it easy to manipulate data for a specific model. The scaffold provides methods and pages that allow you to insert, update, and delete records in your database.

To scaffold theEventmodel, open the fileapp/controllers/events_controller.rbin your editor and addscaffold :eventinside the class body. When you’re finished, the file should look like Listing 3-4.

Listing 3-4. The app/controllers/events_controller.rb File

class EventsController < ApplicationController
  scaffold :event
end

That’s all you need to generate a working scaffold of theEventmodel. Let’s fire up the web server and test it. Start your local web server from the command line (./script/server) and browse to theeventscontroller in your browser.

http://localhost:3000/events

You should see the results displayed in your browser, as shown in Figure 3-2.


Figure 3-2.  Events scaffolding

Click the New event link, and you’ll be taken to a screen where you can enter events. Notice that the URL ishttp://localhost:3000/events/new, which means you’re invoking thenew action on theeventscontroller. Go ahead and add a few events and generally play with it. Figure 3-3 shows an example of an event entered on this screen.


Figure 3-3.  Adding an event

You’ll notice that every time you add an event, you’re redirected back to theindexaction, where you can see all your events listed. You can edit them, delete them, or create new ones. If you click the show link, you’ll be taken to the detail page for that event. You’ve got to admit, that’s a lot of functionality for only a single line of code!

Speed is the key benefit here. Thescaffold declaration allows you to quickly get something running, which is a great way to test your assumptions.


Caution  Scaffolding comes with an important disclaimer. You shouldn’t be using it in production. It exists to help you do exactly what we just did: get something running.  


Please check back for the conclusion to this article series.


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 ARTICLES

- 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 
Support 

Developer Shed Affiliates

 




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