Ajax Form Action - Issue

6 messages Options
Embed this post
Permalink
StubbTX

Ajax Form Action - Issue

Reply Threaded More More options
Print post
Permalink
OK, thanks to michaelGregoire to helping me with the Data validation issue.  I have a new one, however.  I'm testing out the Ajax "add comment" functionality in the book on page 120.  Specifically I'm looking at the code in the form (Listing 8-7).  

<?=$ajax->form('/comments/add','post',array('update'=>'comments'));?>
<?=$form->input('Comment.name');?>
<?=$form->input('Comment.content');?>
<?=$form->input('Comment.post_id',array('type'=>'hidden','value'=>$post['Post']['id']));?>
<?=$form->end('Add Comment');?>

The form is created correctly but it fails with an error:

"The action comments is not defined in controller PostsController"


Looking at the source, I can see why:

<form id="form2050239561" onsubmit="event.returnValue = false; return false;" method="post" action="/posts/comments/add"><fieldset style="display:none;"><input type="hidden" name="_method" value="POST" /></fieldset><script type="text/javascript">
//<![CDATA[
Event.observe('form2050239561', 'submit', function(event) { new Ajax.Updater('comments','/posts/comments/add', {asynchronous:true, evalScripts:true, parameters:Form.serialize('form2050239561'), requestHeaders:['X-Update', 'comments']}) }, false);
//]]>

I'm not sure where it's picking up the "/posts" and adding it to the URL.  I'm looking at the docs over at http://book.cakephp.org/view/208/ajax but nothing is jumping out at me.
StubbTX

Re: Ajax Form Action - Issue

Reply Threaded More More options
Print post
Permalink
OK, found the answer to my own post.  As I kept digging I found some related bugs that are supposed to be fixed by now.  

https://trac.cakephp.org/ticket/3582


I figured it couldn't hurt to try the solution here:

<?=$ajax->form('/comments/add','post',array('update'=>'comments','url' => '/comments/add'));?>

It now worked correctly.  

I did notice that the code used in the comments_controller.php file had an error (I took it directly from the source download when I was having issues).

$comments = $this->Comment->find('all',array('conditions'=>array('post_id'=>$this->data['Comment']['post_id']),'recursive'=>-1));   <---- Last parenthesis missing  


michaelGregoire

Re: Ajax Form Action - Issue

Reply Threaded More More options
Print post
Permalink
You, sir have returned the favor by saving me time as well.

Thanks!


renulRen

Re: Ajax Form Action - Issue

Reply Threaded More More options
Print post
Permalink
In reply to this post by StubbTX
StubbTX, Thank you!!!
Gearoid

Re: Ajax Form Action - Issue

Reply Threaded More More options
Print post
Permalink
In reply to this post by StubbTX
Sorry if this is a stupid question but what is the missing parenthesis in the comments_controller? Trying to figure it out.
Cheers
StubbTX

Re: Ajax Form Action - Issue

Reply Threaded More More options
Print post
Permalink
I was using the code directly from the source code downloads provided on the book's webpage.  The source code was missing a ")" and wouldn't work correctly.