Archive

Archive for August, 2009

CFWheels — Adding a new table with model, view, and controller

August 16th, 2009

In Part 2, we looked at Editing the View in CFWheels.

We thought we were done, but now the boss wants to record events along with ticket sales. Remember we are following along in Head First Rails, and converting everything to CFWheels!

Fortunately, with scaffolding, all we need to do is create the new database table and run the scaffolding plugin.

Here’s the create table SQL query:

CREATE TABLE `events` (
`id` int(11) NOT NULL auto_increment,
`artist` varchar(255) NOT NULL,
`description` text,
`price_low` decimal NOT NULL,
`price_high` decimal NOT NULL,
`event_date` date NOT NULL,
PRIMARY KEY  (`id`)
)

I got the idea from a Rails app to create a “spec” directory to hold application specification information. And, I created a database sub-directory to hold all my table creation SQL. So the query is stored in /spec/database/events.sql.

Next, click the Scaffold link. Look in the debug information at the bottom of each page, next to Plugins. Then, on the scaffold page, enter event as the Object name, and click Generate. Remember, the table is named events (plural with an “s”), but the the object is named event (singular).

All that’s left is to open /views/events/ and edit the labels in edit.cfm, index.cfm, new.cfm and show.cfm. This last step isn’t really needed to make things work, but the boss is kind of picky.

Great — We’ve finished with Chapter 1 of Head First Rails. You now know how to:

  • Create a new CFWheels application
  • Install the scaffolding plugin to CFWheels
  • Setup a database table and  use scaffolding to automatically generate the Model, View, and Controller code.
  • Edit HTML in the View
  • Add fields and new tables to the database

Next time, we’ll start Chapter 2, and build an application from scratch — Without using scaffolding.

Clarke Bishop CFWheels, ColdFusion

Editing the View in CFWheels

August 12th, 2009

In Part 1, we looked at Scaffolding Basics in CFWheels.

Now, I’ll continue working through the exercises in Head First Rails and converting things to CFWheels.

Page 27: Edit the HTML in the View

When you use scaffolding, the labels will be the same as the field names in the database. This may be OK, but if you need to change them to make them more readable by humans, it’s easy enough to edit the HTML in the views.

  • Look at the directory for webroot/views/tickets. You’ll see four files named edit.cfm, index.cfm, new.cfm, and show.cfm. These are the four files that you need to edit.
  • Open up edit.cfm, and you’ll see the code like: <label>Seat_id_seq</label>. Seat_id_seq isn’t a very human-friendly label, so let’s change it to Seat #.
  • Change the code to <label>Seat ##</label>. Remember this is inside a <cfoutput> block, so you have to escape the # symbol by typing it twice.
  • Open index.cfm and change the appropriate line to <cfcol header=”Seat ##” text=”#seat_id_seq#” />.
  • Now make similar changes to new.cfm and show.cfm.
  • Save everything, then go to http://localhost/tickets/index.cfm?controller=tickets&action=index to see your beautiful new labels!

I made one other change to the code to make things work correctly! At the bottom of the show.cfm file, there’s a line that starts out with
#linkTo(text=”Return to the listing”, action=”action”)#. This may be a bug in scaffolding, or maybe I did something wrong. This line should really say:
#linkTo(text=”Return to the listing”, action=”index“)#. This way, the link will go back to the index view.

Page 31: Adding a new data field

The Boss forgot that we need a phone number field, so now we have to add it!

In Rails, you have to do > script/generate migration … and then run > rake db:migrate. In CFWheels, we’ll just run an Alter Table query on the database. Here’s the query I ran:

ALTER TABLE tickets
ADD phone varchar(255)

But, just like with Rails, changing the database isn’t enough (Page 36). We still have to update the code in the View files to handle the new field. Here’s the code I added to edit.cfm and new.cfm:

<p><label>Phone</label> <br />
#textField(objectName=’ticket’, property=’phone’)#</p>

Notice the textField() function. This is the CFWheels equivalent of <%= f.text_field :phone %>. Rails also has the f.label function. I’m not sure of the advantages of using a helper function for labels. But, this doesn’t seem to be available in CFWheels — At least not yet!

By the way, you can get details on any helper function like the textField tag at:

http://code.google.com/p/cfwheels/wiki/textFieldTag

The changes to show.cfm and index.cfm are similar. If you have any trouble, leave me a comment!

I heard a rumor that the Boss wants to add event information to our application. We’ll look at this next time!

Clarke Bishop CFWheels, ColdFusion