Archive

Archive for the ‘CFWheels’ Category

onError Handling for CFWheels and Splash CMS

February 4th, 2010

For most websites it’s usually a good idea to have error pages that have user-friendly error messages that are styled to look like the rest of the site.  Also, you may want to send yourself an eMail when an error happens. Read on to see how to make this work.

For the DonorUp website, I used CFWheels and the Splash CMS. Once I knew the tricks, it turned out to be really easy to make this work. So, I decided to write a post to explain what you need to know.

Typically, in ColdFusion, you add an onError() method to your Application.cfc file. Wheels gives you various hooks into Application.cfc, but Wheels already has an onError() method. So, you can’t add another one! Fortunately, Wheels has an easy way to handle most errors.

How to send error messages from CFWheels

All you have to do to get Wheels to send error message emails  is add the following into your /config/settings.cfm file:

<cfset set(sendEmailOnError = true)>
<cfset set(errorEmailAddress = “you@yourServer.com”)>

Now, whenever there’s an error, you’ll get a really nice email with all the troubleshooting info you’ll need. Easy!

How to setup a custom error page in CFWheels or Splash

As part of its built-in error handling, Wheels runs the file at /events/onerror.cfm. Just put a redirect to your error page at the top of onerror.cfm and you’re done:

<cfset redirectTo(route=”viewer”, slug=”error-page”)>

In my case, I went into the Splash CMS admin and added a page called Error Page. This way, the page is automatically styled to match the rest of the site. The error page apologizes to the user for the error, and let’s them know I am going to fix it!

When Wheels does its error handling, it will run /events/onerror.cfm, and the redirectTo() will send them to my error page! If you aren’t running Splash, you could just use the other arguments of redirectTo() to send them to any controller and action.

You can also put your own HTML or CFML code in onerror.cfm, but I like doing it this way better as it uses all the layouts and styling of my site!

Please leave me a comment if you like this approach or have other ideas!

Clarke Bishop CFWheels, ColdFusion, Splash CMS ,

CFWheels — Using Layouts and Stylesheets

September 6th, 2009

On Page 73 of Head First Rails, we need to add a design for the meBay site.

Just like Rails, CFWheels supports layouts to create a master template and to avoid repeating yourself.

For our simple app, we could just work with the default layout located at /views/layout.cfm. But, to follow the book, let’s create a controller-specific layout at: /views/ads/layout.cfm. So, this layout.cfm in the /view/ads/ subdirectory will only be used for views associated with the ads controller.

[cf]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<cfoutput>
<title>Ads: #params.action#</title>
#stylesheetLinkTag("default")#
</cfoutput>
</head>
<body>
<cfoutput>
<div id="wrapper">
<div id="header">
<div>
<h1>MeBay</h1>
<ul id="nav">
<li>#linkTo(text="All Ads", route="adsNoKey")#</li>
</ul>
</div>
</div>

<div id="content">
#contentForLayout()#
</div>
<div id="clearfooter"></div>
</div>
<div id="footer"></div>
</cfoutput>
</body>
</html>
[/cf]

Most of this code is HTML boilerplate. #params.action# inserts the name of the current action in the Title tag. The params structure is always passed from the controller to the view. It includes the name of the action, variables, etc. You can always cfdump it out if you want to see what all is in there.

#stylesheetLinkTag(“default”)# uses another CFWheels helper function. By convention, stylesheets are located in the /stylesheets directory. So this function builds the link to our stylesheet named default.css. Download the code for Chapter 2 from the Head First Rails website, and get default.css from the /public/stylesheets/ folder. In CFWheels, put default.css in your /stylesheets folder. If you have trouble with the stylesheet, make sure all the images references are correct. If your web server configuration is different, you might need to adjust the paths.

On line 16, we’ve setup a link to our index action.

On line 22, you see #contentForLayout()#. This is where CFWheels puts the content from the other view pages — show.cfm and index.cfm. It’s the equivalent of <%= yield %> in Rails.

That’s it! Now we have a pretty, styled meBay application. Visit http://localhost/index.cfm/ads/ and see how pretty things look.

Next, it’s on to Chapter 3. In software development, things always change. And, now meBay wants to let people post ads online!

Clarke Bishop CFWheels, ColdFusion