Editing the View in CFWheels
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!
