2Dec/0621
Undefined variable: javascript in cakePHP with scriptaculous
This weekend I was trying to get scriptaculous work with cakephp. Unfortunately I did always end up with the same error:
[code]Undefined variable: Javascript[/code]
After a couple of hours searching I found the following things can cause this problem:
- You do not have the html, javascript and ajax helper defined in your own controller (eg. user_controller.php)[php]var $helpers = array('Html','Javascript','Ajax'); [/php]
- Your render action points to a view where no view file is existing for. ( so the view file HAS to be created![php]$this->render('the_method','ajax'); [/php]
Hope it helps somebody who is experiencing the same problem I had.
February 13th, 2007 - 01:40
totally perfect. ran into the same problem. thx.
March 28th, 2007 - 08:20
Safed me perhaps hours of searching thanks
March 30th, 2007 - 00:55
Thank you very much! It took me about two hours to find the answer
April 15th, 2007 - 07:24
I floundered around with this as well, following the tutorial at http://grahambird.co.uk/cake/tutorials/ajax.php
I solved it by copying the vanilla app_controller.php file from /cake/ to /app/ and adding the helpers info (#1) there. I’m new to Cake but I presume that as the $javacript reference is in the default view, that variable has to be set up in the generic controller.
Thanks for the tip- this saved me a few hours…
July 6th, 2007 - 07:43
Also this and similar things can happen if your database is out of synchron. For example, i’m developing at my local server, and doing svn commits to another server. The commit invokes a script that immediatly updates the server version from the repository, and sometimes I can get similar messages if I don’t care enough to sync the databases.
July 10th, 2007 - 04:50
This was a big help! The tutorial I was following also left out the $helpers var, so this was very useful.
July 20th, 2007 - 15:21
Thanks, saved me some time
August 23rd, 2007 - 04:45
Thanks so much for documenting this and finding a solution! I had been freaking out for the past hour trying to figure out where I went wrong. Your solution fixed this perfectly!
November 6th, 2007 - 10:26
Thanks u saved me precious time
November 16th, 2007 - 20:11
Yeah, I’ve run into the undefined variable: javascript a bunch of times… Finally figured out that if you are defining your helper array in app_controller.php & you happen to be missing a model or have an improperly named db table, the error gets reported in the title of the page, but the view displays the undefined var: javascript. In a nutshell… make sure that your app_controller is entirely valid if yr defining helpers there.
December 13th, 2007 - 09:46
a great help
December 13th, 2007 - 21:56
Big help!!! Thank you for posting.
February 8th, 2008 - 17:50
Thank you!!!! I was also following the ajax/scriptaculous tutorial and ran into that!
February 22nd, 2008 - 18:20
Thank you! I was trying for a while – then I used ‘BCC’ answer.
April 10th, 2008 - 07:17
I stuck with the same problem for a while. Thanks for the post, I found it very usefull.
April 16th, 2008 - 09:10
Thanks for great help in absence of proper documentation for cakphp
April 16th, 2008 - 09:12
Thanks for great help in absence of proper documentation cakephp
October 17th, 2008 - 21:24
Thanks, you save my day!!
November 3rd, 2008 - 18:52
I was experiencing this same problem, but it was firing at my default layout. To remedy this, I had to put it in my app_controller.php file so it was available at a higher level.
class AppController extends Controller {
var $helpers = array('Html', 'Javascript');
}
Remedied all my issues. Thanks for pointing me in the right direction.
June 10th, 2009 - 15:02
thanks lot..helped me lot in development
September 24th, 2009 - 05:18
I thought I should add to this:
Went through the same problem and this post helped me figure out the cause (trying to render a non-existent view).
The thing is I was messing with custom flash message layouts, and the non-existent view was a custom flash layout that was specified earlier, but then changed. No matter how many times I reloaded the page, it kept the same way. I deleted all the cache files, even restarted Apache hoping that something was being cached in the memory level. Nothing worked.
Only after some 20 minutes I realized the name of the custom layout was written to the session itself, so regardless of me having changed it in my code, it was still being called.
The solution was to create the non-existent file temporarily and reload. It worked. I then removed this file and tried again: everything worked perfectly.
Just keeping this here in case it’s helpful to anyone.
Cheers