Note that there are some explanatory texts on larger screens.

plurals
  1. POCakePHP Form action from other controller
    primarykey
    data
    text
    <p>Well, I have a CakePHP 2.2.5 app which is basically management of users, and a user has a photo, and I create a PhotosController (as suggestion of this <a href="http://goo.gl/oglQn" rel="nofollow">tutorial</a>). I tested it and it works perfectly on <code>Photos/View</code>, including the ajax elements to be possible send a photo in a form without redirecting. And now I'm trying to connect all of this, but it ins't as easy as I think it should be.</p> <p>My Photo add method:</p> <pre><code>public function add() { if ( $this-&gt;request-&gt;is( 'post' ) ) { $this-&gt;Photo-&gt;create(); $json = array( 'result' =&gt; array( 'files' =&gt; array() ) ); if ( $this-&gt;Photo-&gt;save( $this-&gt;request-&gt;data ) ) { $photo = $this-&gt;Photo-&gt;findById( $this-&gt;Photo-&gt;id ); $json['files'][0]['name'] = $photo['Photo']['basename']; $json['files'][0]['size'] = $this-&gt;request-&gt;data['Photo']['file']['size']; $json['files'][0]['url'] = $this-&gt;webroot . 'media/transfer/img/' . $photo['Photo']['basename']; $json['files'][0]['thumbnail_url'] = $this-&gt;webroot . 'media/filter/thumbnail/img/' . $photo['Photo']['basename']; } else { $json = 'Error'; $this-&gt;Session-&gt;setFlash( __( 'The photo could not be saved. Please, try again.' ) ); } $this-&gt;RequestHandler-&gt;renderAs( $this , 'ajax' ); Configure::write( 'debug' , 0 ); $this-&gt;set( 'json' , json_encode( $json ) ); $this-&gt;render( '/Elements/ajax' ); } </code></pre> <p>My test View (on users controller): </p> <pre><code>&lt;?php echo $this-&gt;Form-&gt;create( 'Photo' , array( 'url' =&gt; array( 'controller' =&gt; 'photos' , 'action' =&gt; 'add' ) , 'id' =&gt; 'fileupload' , 'enctype' =&gt; 'multipart/form-data' ) ); ?&gt; &lt;-- Some HTML HERE--&gt; &lt;?php echo $this-&gt;TB-&gt;input( 'Photo.file' , array( 'prepend' =&gt; 'Upload' , 'type' =&gt; 'file' , 'class' =&gt; 'fileUpload' , //'multiple' =&gt; 'multiple', 'div' =&gt; FALSE , 'between' =&gt; FALSE , 'after' =&gt; FALSE , 'label' =&gt; FALSE , 'help' =&gt; NULL , ) ); ?&gt; &lt;/form&gt; &lt;-- Some (already tested) Javascript to make this work HERE--&gt; </code></pre> <p>The view renders without php errors, but when I upload a file I get an Javascript error, and the debug says:</p> <pre><code>GET http://&lt;server&gt;/&lt;app&gt;/users/add 404 (Not Found) jquery.min.js:1960 send jquery.min.js:1960 b.extend.ajax jquery.min.js:1840 (anonymous function) main.js:59 c jquery.min.js:215 p.fireWith jquery.min.js:249 b.extend.ready jquery.min.js:69 H jquery.min.js:10 </code></pre> <p>Actually, I get this error on both, GET and POST methods (with some different line of jQuery of course).</p> <p>Even the action is set to the PhotosController it is trying to access the Users. I think because of the view, as I din't specify users anywhere. I am trying to solve this problem for a week and I don't have more ideas. Any help would be greatly appreciated. </p> <p>Thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload