Newbie ajax confusion while ostensibly following Cookbook....
#1
Posted 11 February 2010 - 09:53 PM
I'm trying to do a very simple renderPartial using an ajaxLink, and have really found it hard to find a simple explanation on the site.
I followed the code from the example, and nothing happens, and when I look at the generated code for the ajaxLink it shows up as <a href="#" id="yt0">Another Comment</a>
I put in the original view (essentially right from the Cookbook page) view.php:
<div id="data">
<?php $this->renderPartial('_anothercomment', array('myValue'=>$myValue)); ?>
</div>
<?php echo CHtml::ajaxLink("Another Comment", CController::createUrl('album/UpdateAjax'), array('update' => '#data'));
?>
and then on the controller page AlbumController.php:
public function actionUpdateAjax()
{
$data = array();
$data["myValue"] = $model->reviewquote_2;
$this->renderPartial('_anothercomment', $data, false, true);
}
and on the partial: _anothercomment.php
<?php echo $myValue;?>
Could anyone tell what I'm doing wrong?
Thanks
michael
#2
Posted 11 February 2010 - 10:57 PM
Are you seeing any javascript errors ? Firefox with Firebug are an excellent combination to verify if your ajax calls are being performed and what the response is.
nz
#3
Posted 12 February 2010 - 09:00 AM
no firebug errors are shown, but I get no response either. What I am trying to do is get additional data from a field in a table, I think that the code in the controller is OK with:
$data = array();
$data["myValue"] = $model->reviewquote_2;
$this->renderPartial('_anothercomment', $data, false, true);
and I know that the field isn't null, so I don't think that is the issue. Just to check I added another value to the data array that was just a string, but that didn't show up either.
frustrating! and I have been really enjoying working with yii the past week
Thanks for the clue on firebug, it looks very useful. I had gotten into the habit of using Chrome, I may be using firefox a lot more
#4
Posted 12 February 2010 - 09:13 AM
Quote
The $model object needs to be populated on the Ajax call.
You can see the Ajax requests under "Console" in Firebug.
/Tommy
This post has been edited by tri: 12 February 2010 - 09:16 AM
#5
Posted 12 February 2010 - 09:26 AM
here is the relevant section of the link shown...
/index.php?r=album/UpdateAjax&_=1265984429221
and the message is 403 aborted
subsequent clicks have a different number appended and throw a 403 CHttpException
very interesting - I have no idea where the "&_=1265984429221" comes from!
the response is
You are not authorized to perform this action.
so i will look at the accessRules()
#6
Posted 12 February 2010 - 09:52 AM
Michael Sullivan, on 12 February 2010 - 09:26 AM, said:
and the message is 403 aborted
...
very interesting - I have no idea where the "&_=1265984429221" comes from!
...
Do you use access rules and forgot to add "updateajax"?
The "&_=1265984429221" string is appended by jQuery to avoid caching (unique url on every call).
(IIRC)
/Tommy
#7
Posted 12 February 2010 - 10:29 AM
The requested page does not exist.
Now, in looking at the cookbook, my understanding is that the CController::createUrl('album/UpdateAjax') portion of the ajaxLink() in my original script view.php works like this:
'album/UpdateAjax' translates to "look in AlbumController.php for the actionUpdateAjax function and then render a chunk of code from the file named in the renderPartial() portion of the actionUpdateAjax function:
$this->renderPartial('_anothercomment', $data, false, true);
so I am assuming that the renderPartial would call _anothercomment.php from the appropriate view - in this case album/_anothercomment.php
except that in the params portion of firebug the non-existing page is
http://..... /michael/index.php?r=album/UpdateAjax&_=126598637644
so I guess I'm still confused!
#8
Posted 12 February 2010 - 12:01 PM
NZ
#9
Posted 12 February 2010 - 12:13 PM
I changed access rules again,to wide open
array('allow',
'users'=>array('*'),
but got the same "The requested page does not exist."
the "http://localhost/index.php?r=album/UpdateAjax" is the URL for the controller, not the view
the function actionUpdateAjax in AlbumController.php would then call the view _anothercomment.php , right?
based on the previous accessrules error, it IS calling AlbumController.php, or else it wouldn't have generated that error, I just can't figure out why renderPartial isn't calling for the _anothercomment.php file
#10
Posted 12 February 2010 - 12:27 PM
public function accessRules() {return array('allow', 'users'=>array('*'),); }
Do you define the function "actions" in your controller ?
NZ
#11
Posted 12 February 2010 - 12:40 PM
array('allow', // allow all users to perform 'index' and 'view' actions
'actions'=>array('index','view','updateajax'),
'users'=>array('*'),
),
array('allow', // allow authenticated user to perform 'create' and 'update' actions
'actions'=>array('create', 'update'),
'users'=>array('@'),
),
array('allow', // allow admin user to perform 'admin' and 'delete' actions
'actions'=>array('admin','delete'),
'users'=>array('admin'),
),
array('deny', // deny all users
'users'=>array('*'),
),
after adding 'updateajax' to the allow rule in the first section, the error went away, and was replaced by "The requested page does not exist."
I then further changed it to
array('allow',
'users'=>array('*'),
after commenting everything else out. No access errors, but still the file not found error
#12
Posted 12 February 2010 - 12:55 PM
1) For a quick solution try echoing the string from the controller action instead of rendering the view
2) Enable logging and add calls to Yii::trace()
The blog tutorial has this description:
http://www.yiiframew...g/final.logging
(remember to add trace and info levels)
You will find the log file under protected/runtime
For further reading about logging see this section in the guide
http://www.yiiframew.../topics.logging
/Tommy
#13
Posted 12 February 2010 - 01:00 PM
If so comment it out.
nz
#14
Posted 12 February 2010 - 01:19 PM
also I did add updateajax to the actions in the accessRules() array
array('allow', // allow all users to perform 'index' and 'view' actions
'actions'=>array('index','view','updateajax'),
'users'=>array('*'),
),
the other action functions (actionUpdate actionCreate etc) created by yiic in the controller seem to work fine
I was just following the cookbook - which didn't remind me of the need to change accessRules(), so I just essentially adapted the instructions and tried to run it.
#15
Posted 12 February 2010 - 03:41 PM
Michael Sullivan, on 12 February 2010 - 01:19 PM, said:
array('allow', // allow all users to perform 'index' and 'view' actions
'actions'=>array('index','view','updateajax'),
'users'=>array('*'),
),
...
I was just following the cookbook - which didn't remind me of the need to change accessRules(), so I just essentially adapted the instructions and tried to run it.
I think this is the key to why the mixed case URL didn't work
Quote
---------------------
- Application and module parameter names are changed to be case-sensitive.
In 1.0.x, they are case-insensitive.
http://code.google.c...e/trunk/UPGRADE
It seems like you verified that lower case is a requirement (feel free to comment or update the cookbook article). A note about the need for adding new actions to accessRules() may also be appropriate.
(Qiang, the convention section of the guide may also need some attention)
/Tommy
#16
Posted 12 February 2010 - 04:07 PM
tri, on 12 February 2010 - 03:41 PM, said:
http://code.google.c...e/trunk/UPGRADE
It seems like you verified that lower case is a requirement (feel free to comment or update the cookbook article). A note about the need for adding new actions to accessRules() may also be appropriate.
(Qiang, the convention section of the guide may also need some attention)
/Tommy
Well, I changed the ajaxLink variable to album/updateajax , the controller function from actionUpdateAjax to actionupdateajax, and I still get "The requested page does not exist." when I look at the console in Firebug.
here is the stack trace:
Stack trace:
#0 /home/garuda/public_html/michael/protected/controllers/AlbumController.php(177): AlbumController->loadModel()
#1 /home/garuda/public_html/yii/framework/web/actions/CInlineAction.php(32): AlbumController->actionupdateajax()
#2 /home/garuda/public_html/yii/framework/web/CController.php(300): CInlineAction->run()
#3 /home/garuda/public_html/yii/framework/web/filters/CFilterChain.php(129): CController->runAction(Object(CInlineAction))
#4 /home/garuda/public_html/yii/framework/web/filters/CFilter.php(41): CFilterChain->run()
#5 /home/garuda/public_html/yii/framework/web/CController.php(983): CFilter->filter(Object(CFilterChain))
#6 /home/garuda/public_html/yii/framework/web/filters/CInlineFilter.php(59): CController->filterAccessControl(Object(CFilterChain))
#7 /home/garuda/public_html/yii/framework/web/filters/CFilterChain.php(126): CInlineFilter->filter(Object(CFilterChain))
#8 /home/garuda/public_html/yii/framework/web/CController.php(283): CFilterChain->run()
#9 /home/garuda/public_html/yii/framework/web/CController.php(257): CController->runActionWithFilters(Object(CInlineAction), Array)
#10 /home/garuda/public_html/yii/framework/web/CWebApplication.php(320): CController->run('updateajax')
#11 /home/garuda/public_html/yii/framework/web/CWebApplication.php(120): CWebApplication->runController('album/updateaja...')
#12 /home/garuda/public_html/yii/framework/base/CApplication.php(135): CWebApplication->processRequest()
#13 /home/garuda/public_html/michael/index.php(11): CApplication->run()
#14 {main} REQUEST_URI=/michael/index.php?r=album/updateajax&_=1266008343445
2010/02/12 13:00:00 [trace] [system.web.CModule] Loading "errorHandler" application component
so I changed the ajaxLink and controller function back to how I had them before
same result - "The requested page does not exist." from Firebug
here's the stack trace:
Stack trace:
#0 /home/garuda/public_html/michael/protected/controllers/AlbumController.php(177): AlbumController->loadModel()
#1 /home/garuda/public_html/yii/framework/web/actions/CInlineAction.php(32): AlbumController->actionUpdateAjax()
#2 /home/garuda/public_html/yii/framework/web/CController.php(300): CInlineAction->run()
#3 /home/garuda/public_html/yii/framework/web/filters/CFilterChain.php(129): CController->runAction(Object(CInlineAction))
#4 /home/garuda/public_html/yii/framework/web/filters/CFilter.php(41): CFilterChain->run()
#5 /home/garuda/public_html/yii/framework/web/CController.php(983): CFilter->filter(Object(CFilterChain))
#6 /home/garuda/public_html/yii/framework/web/filters/CInlineFilter.php(59): CController->filterAccessControl(Object(CFilterChain))
#7 /home/garuda/public_html/yii/framework/web/filters/CFilterChain.php(126): CInlineFilter->filter(Object(CFilterChain))
#8 /home/garuda/public_html/yii/framework/web/CController.php(283): CFilterChain->run()
#9 /home/garuda/public_html/yii/framework/web/CController.php(257): CController->runActionWithFilters(Object(CInlineAction), Array)
#10 /home/garuda/public_html/yii/framework/web/CWebApplication.php(320): CController->run('UpdateAjax')
#11 /home/garuda/public_html/yii/framework/web/CWebApplication.php(120): CWebApplication->runController('album/UpdateAja...')
#12 /home/garuda/public_html/yii/framework/base/CApplication.php(135): CWebApplication->processRequest()
#13 /home/garuda/public_html/michael/index.php(11): CApplication->run()
#14 {main} REQUEST_URI=/michael/index.php?r=album/UpdateAjax&_=1266008536360
2010/02/12 13:03:13 [trace] [system.web.CModule] Loading "errorHandler" application component
so I don't think that the case sensitivity is the issue.
Thanks for all your help everybody though - I will get this figured out somehow!
michael
#17
Posted 12 February 2010 - 04:38 PM
Michael Sullivan, on 12 February 2010 - 04:07 PM, said:
...
The controller action method names should still be in camelCase. Only the route part of the url behaves differently. BTW, all lower case in the route was always valid.
Edit: trying to find the reference for the above. From the subsection Route in the Fundamentals - Controller section of the guide:
Quote
/Tommy
This post has been edited by tri: 12 February 2010 - 05:09 PM
#18
Posted 12 February 2010 - 05:20 PM
well, I tried both ways and it didn't work from either one... still "The requested page does not exist."
Does anything jump out at you from the stack trace?? it looks to me like Jquery is still calling the URI from the call to the controller rather than the one I want, which is view/album/_anothercomment.php and which I understood would be created by the renderPartial() call in actionUpdateAjax
{main} REQUEST_URI=/michael/index.php?r=album/UpdateAjax&_=1266008536360
Once again, the example in the Cookbook leads me to believe thats the way it is supposed to work - please correct me if I am wrong!!
#19
Posted 12 February 2010 - 07:13 PM
Michael Sullivan, on 12 February 2010 - 05:20 PM, said:
well, I tried both ways and it didn't work from either one... still "The requested page does not exist."
Does anything jump out at you from the stack trace?? it looks to me like Jquery is still calling the URI from the call to the controller rather than the one I want, which is view/album/_anothercomment.php and which I understood would be created by the renderPartial() call in actionUpdateAjax
{main} REQUEST_URI=/michael/index.php?r=album/UpdateAjax&_=1266008536360
Once again, the example in the Cookbook leads me to believe thats the way it is supposed to work - please correct me if I am wrong!!
Sorry, I didn't pay attention to the stack traces before. It seems clear that action UpdateAjax is entered in both cases. What's loadModel()? That's the last method called. Do you call $this->loadModel(), is the method defined in the controller. What's in it?
Also, as already suggested, try with view rendering excluded, just echo a string from the controller.
echo 'aString'; Yii::app()->end(); // or you can call die;
/Tommy
#20
Posted 12 February 2010 - 07:30 PM
here is the code for loadModel and for actionUpdateAjax. I want to get some data from the AR, and thought I needed to call loadModel() but notice that if the model is null that 'The requested page does not exist.' is thrown
notice that I commented out the loadModel call in actionUpdateAjax -- when I just ran it, I did not get the error. But I STILL didn't get the data I wanted for AR either. So I'm probably not accessing that data properly....
BTW here's the info on $_model (this code generated by yiic)
/**
* @var CActiveRecord the currently loaded data model instance.
*/
private $_model;
public function loadModel()
{
if($this->_model===null)
{
if(isset($_GET['id']))
$this->_model=Album::model()->findbyPk($_GET['id']);
if($this->_model===null)
throw new CHttpException(404,'The requested page does not exist.');
}
return $this->_model;
}
public function actionUpdateAjax()
{
//$model=$this->loadModel();
$data = array();
$data["myValue"] = $model->reviewquote_2;
$this->renderPartial('_anothercomment', $data, false, true);
}
would I need to pass the id variable from the ajaxLink call in view.php along with the data about the controller file?

Help













