I finally started playing with rails again. I decided to play around and start the basics of a billing application. I was initially debating web app versus a desktop app (rails vs ruby-gtk). I wanted a reason to finally start using rails so I decided to make it a web app. I then realized I may be able to simulate a desktop app with rails. I pulled in the base ruby packages, manually compiled RubyGems and installed necessary gems, and decided to script a startup routine so I could create a launcher on my desktop. This solution is far from perfect but it manages to fulfill my needs. My “desktop rails app” uses the built in standalone webserver WEBrick and sqlite3 for its backend.
#!/bin/bash cd /home/jesse/rails/billing if [ ! -f tmp/pids/server.pid ]; then script/server -d fi while [ `netstat -tan | grep 0.0.0.0:3000 | wc -l` != 1 ]; do echo "Server not started yet..." sleep 1 done google-chrome http://localhost:3000/customers & tail -f log/development.log # Server can be killed with: # kill -INT `cat tmp/pids/server.pid`
I then created a launcher simliar to this:
When I double click on the icon the first time it pauses for about 5-6 seconds while WEBrick initializes and binds to port 3000. As soon as it detects the webserver listening, I launch google-chrome to the default view. Each time I click the icon there after, the webserver is already running so the google-chrome tab kicks off instantly.
Mocha is written entirely in CakePHP and jQuery and is certainly the best CakePHP eCommerce app out there. Why do I say that?
Mocha Shopping Cart is…
If you still don’t believe me, Mocha is offering a limited time only Pre-Launch coupon code that gives you the first month FREE. You can try it for yourself and see how you like it. I think you will agree that it beats CakeCart and BakeSale, HANDS DOWN! And no, Mocha is not free (after the first 30 days). The costs help to speed up development, pay for server and hosting fees, and pay for that cup of coffee that us developers need when its 8 AM and we are having to talk with <insert_API_name_here> first thing in the morning.
Official Website: Mocha Shopping Cart
Blog with coupon code: Get a free trial of Mocha Shopping Cart
I am not sure if this is the fanciest way to get the job done but it works REALLY well. What job am I referring to? I am referring to outputting data as JSON in a CakePHP view and then parsing it in a jQuery AJAX response. Its fairly easy to accomplish with the JSON parser provided by json.org. Scroll to the bottom and download/include json2.js.
Controller example:
$sizeArray = getimagesize('/path/to/really_cool_image.jpg');
$outputArray = array(
'file' => 'really_cool_image.jpg',
'width' => $sizeArray[0],
'height' => $sizeArray[1],
);
$this->set('result', $outputArray);
$this->render(null, 'ajax');
AJAX View example:
Configure::write('debug', 0);
echo $javascript->object($result);
jQuery AJAX response handler example:
function(response, status) {
imageDetails = JSON.parse(response);
imageName = imageDetails.file;
}
There are currently three methods for including and using models inside your controller. I am referring to the case when your controller does not belong to a model or or the model is not related to the model inside the controller you are working in. Although this case should be extremely rare, it seems to happen to me a lot. This post is basically a re-summary of Gwoo’s response in the CakePHP Google Groups discussion.
1. Controller::loadModel();
This method actually calls ClassRegistry::init() and saves the instance of the model as a parameter in the controller.
Example:
Controller::loadModel('Car');
$cars = $this->Car->find('all');
2. ClassRegistry::init()
This method creates an instance of the Model and returns it for use.
Example:
$car = ClassRegistry::init('Car');
$cars = $car->find('all');
3. App:import() (not recommended)
This method only includes the file. This requires you to create an instance each time.
Example:
App::import('Car');
$car = new Car();
$cars = $car->find('all');
As you can see this is really simple and basic stuff. However, I felt the need to re-post this information. If for nothing else, I will be using this as a reference point for myself
Update 5/27/09: Miles Johnson has a nice write up on this on his blog
I was having some issues today with character encoding and I thought I’d post about it. There are three elements that you need to make sure you have if you want to properly use UTF-8 character encoding in your Cake app. My issue was that I did not have #2, although adding #1 didn’t hurt either.
1. Database Config
Add ‘encoding’ => ‘utf8′ to app/config/database.php
var $default = array( 'driver' => 'mysql', 'persistent' => false, 'host' => 'localhost', 'login' => 'user', 'password' => 'password', 'database' => 'database', 'encoding' => 'utf8' );
2. Make sure your layout has the charset line in the
<head> <?php echo $html->charset(); ?> Other elements... </head>
3. This one is set by default, but make sure App.encoding is set in app/config/core.php
/**
* Application wide charset encoding
*/
Configure::write('App.encoding', 'UTF-8');
And thats it!