CFWheels Scaffolding Basics
I choose CFWheels as my first ColdFusion framework to learn. I was going to learn ColdBox, but quickly discovered that I would really have to learn Transfer or Reactor and Coldspring at the same time. These may be good tools, but I wanted to focus mostly on mastering the Model View Controller pattern.
I’ve been really pleased with CFWheels. It’s the ColdFusion answer to Ruby on Rails, and it just makes sense to me!
The documentation is good. Still, for a new learner, some things are missing. So, I thought maybe I can leverage some Rails resources. I found a great book — Head First Rails. I’m going to do some posts on translating the book to CFML and to CFWheels. I think this will help my learning and may help others.
From here, I’m assuming that you know something about web development, CFML, and already have a ColdFusion server running on your computer. If not, this information may be too advanced for you. CFWheels runs great on the development edition of ColdFusion that’s available from Adobe. Railo is also supported. You can learn more about the requirements at: http://cfwheels.org/docs/chapter/requirements.
Now on to Head First Rails, Chapter 1: Really Rapid CFWheels
Page 7: Creating a New Application
In Rails, you open a command prompt and type > rails tickets. This sets up the folder structure for your app.
In CFWheels, you do the same thing by downloading the CFWheels files and just unzipping them to a folder named tickets in your webroot. The name of the folder is very important. CFWheels and Rails both rely on convention instead of configuration. Things have to be named consistently!
Page 14: Creating Tables (Migration)
In Rails, you create tables in the database by going to a command prompt and typing > ruby script/generate scaffold tickets … Then, you have to run a migration by typing > rake db.migrate. These two commands also write code for your application! Rails will automatically create Model, View, and Controller files for your new table.
CFWheels handles this a little differently, but you can quickly get to the same place. First, you have to install the Scaffolding plugin.
- Download the scaffolding plugin
- Save the .zip file in your wwwroot/tickets/plugins folder.
- Reload CFWheels by running this URL in your browser: http://localhost/tickets/?reload=true
- CFWheels will automatically unzip and install the file! You’ll see Scaffold listed as a plugin at the bottom of the page along with other debugging information.
- NOTE: Apparently there’s a problem with the Plugin Manager right now. I had to delete the Plugin Manager files from the plugins directory. Then, this process worked for me. I discovered the problem was with my ColdFusion server configuration. I had added a Dump() method to Component.cfc. When Wheels tried to load multiple plug-ins, it saw the duplicated method, and threw an error as a result of the naming conflict.
You’ll only need to do the steps above once. Now, lets use scaffolding to create an app in CFWheels. Instead of using the script/generate syntax, CFWheels expects you to just create your table via a SQL query or whatever database tools you like to use. Most developers know how to do this, and I like this approach better than learning a new syntax in Rails!
Here’s my Create Table SQL script. This is essentially the same command you type into Rails via script/generate.
CREATE TABLE `tickets` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(255) NOT NULL,
`seat_id_seq` varchar(255) NOT NULL,
`address` text NOT NULL,
`price_paid` decimal NOT NULL,
`email_address` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
)
Now, it get’s really easy. Either click the Scaffold link on the bottom of your browser screen, or point your browser to http://localhost/tickets/index.cfm?controller=wheels&action=plugins&name=scaffold. This will display the scaffolding plugin screen.
Just enter the name of the table and click Generate. That’s it! Instant Application. Go to http://localhost/tickets/index.cfm?controller=tickets&action=index to see your new application. Rails automatically creates a route, so their URL is simpler, and you can easily do the same thing if you want. For me, when learning, I’d rather see the controller and action clearly shown in the URL. There’s always time to switch to pretty URL’s later on.
Next we’ll edit the HTML in the view. But, that’s another post. Please leave a comment if I got something wrong or you like my approach!
