CFWheels — Fixing the Controller and View
Last time, we setup our route, and now if you go to: http://localhost/index.cfm/ads/3, you won’t get an error. But, just like on Page 63 of Head First Rails, the ads are blank and the page is kind of boring — There’s no data.
First, we need to fix our controller at /controllers/Ads.cfc. Make sure your show function looks like:
[cf]
<cffunction name="show">
<!— Find the record —>
<cfset ad = model("Ad").findByKey(params.key)>
</cffunction>
[/cf]
When Wheels created the model, it automatically adds the findByKey method. All we have to do is tell it which key we want, and params.key has the key value!
We still need to fix /views/ads/show.cfm to display the data. Change this file to:
[cf]
<cfoutput>
<p><b>Name:</b>#ad.name#</p>
<p><b>Description:</b>#ad.description#</p>
<p><b>Price:</b>#ad.price#</p>
<p><b>Seller Id:</b>#ad.seller_id#</p>
<p><b>Email:</b>#ad.email#</p>
<p><img src="#ad.img_url#"/></p>
</cfoutput>
[/cf]
Pretty neat. Just put the values in the view and wrap it up in a <cfoutput> tag! Wheels handles the hard part of getting the data from the database into the model.
Go back to: http://localhost/index.cfm/ads/3, and you’ll see that the data is now being correctly displayed.
