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:
Looks right to me, the code generated for the ajax link is ok.
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.
I can see the javascript for the ajax call in Firebug, but i see no info on response… but I have never used Firebug before - the wiki talks about a 1.5x version that is the "tracing version" but on the download site I was only able to get 1.5
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:
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
I changed the accessrules() to allow ‘updateajax’ and got:
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:
Your right in your understanding. I think your access rules are your problem, can you comment them out and see if it works ? Specifically this request http://localhost/index.php?r=album/UpdateAjax should not give you a 403 error. If it does work post your access rules .
I had changed the access rules previously, and the firebug response "You are not authorized to perform this action." went away. It was replaced by "The requested page does not exist."
I changed access rules again,to wide open
array('allow',
'users'=>array('*'),
but got the same "The requested page does not exist."
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
at first i got the error "You are not authorized to perform this action." when trying to invoke album/UpdateAjax then I changed accessrules to :
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
Sorry. I do not define the function "actions" - if by that you mean actions()
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.
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)
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.
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:
Note the difference between inline actions (has to start with "action") and external action classes.
regarding the naming, I gathered that from the way things were named in the accessRules()
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
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;
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?