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).
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.
app/controllers/events_controller.rb: The controller that will be responsible for handling requests and responses for anything to do with events.
test/functional/events_controller_test.rb: The class that will contain all functional tests for theeventscontroller (we’ll cover testing applications in Chapter 8).
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.
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.