Monday, November 3, 2008

Undefined variable: javascript (..default.ctp)

This error was causing me fits as it was not really throwing an error on the screen, only in my debug.log. What I found was that this was coming from not having my reference in the right controller. Each of my controllers had a similar reference:

class FaqsController extends AppController {
var $name = 'Faqs';
var $helpers = array('Html', 'Form', 'Javascript', 'Fck');
....
}

The error I was seeing was being generated by my default layout. All I had to do to fix this was put the following in my app_controller.php file.

class AppController extends Controller {
var $helpers = array('Html', 'Javascript');
}

Thanks to the V-TEK Channel blog and this link for pointing me in the right direction.

4 comments:

  1. This error also occurs when one of the helpers in the $helpers array is non existent or misspelled. So if you have var $helpers = array('Javascript', 'XYZ'); and XYZ does not exist, you will still receive an error message about 'Javascript' helper missing, which is quite misleading.
    Took me some hours to find out, hope to save some time for others.

    ReplyDelete
  2. Hey man!
    I have a problem My Code Is working
    but the JavaScript does not get included..

    please let me know what changes I need to do .
    here is my code in the view page.

    echo $javascript->link('/app/views/layouts/js/jQueryValidation/jquery.validate.js');


    Regards
    Sushant danekar
    http://sushant-danekar.blogspot.com/

    ReplyDelete
  3. Sushant,

    I believe you have your JavaScript validation file in the wrong place. The file you are trying to reference with $javascript->link would typically be located in "/app/webroot/". The Javascript helper by default looks in the "js" folder under webroot. So initially, unless there is some real specific reason you have this under "/app/views/layouts", I would move the folder "jQueryValidation/jquery.validate.js" under this folder:
    /app/webroot/js

    Then your code should look like this:

    echo $javascript->link('jQueryValidation/jquery.validate.js');

    ReplyDelete
  4. Yes. For 2.x We have to use

    echo $this->Html->script("jquery-1.4.2.min");

    Very nice tutorials :)

    ReplyDelete