views:

17

answers:

1

Simple question from an absolute n00b.

I'm trying to create an edit content screen based on the code given in the Blog Tutorial on the Cake site.

Here's my edit function in the controller (it's named Contents):

function edit( $id = null ) {
    $this->Content->id = $id;
    Debugger::dump( $this->Content->id );
    if( empty( $this->data ) ) {
        $this->data = $this->Content->read();
        Debugger::dump( $this->Content );
    }
    else {
        if( $this->Content->save( $this->data ) ) {
        $this->Session->setFlash( 'Content has been updated.' );
        $this->redirect( array('action' => 'index') );
        }
    }
}

And this is edit.ctp:

echo $form->create( 'Content', array('action' => 'edit') );
echo $form->input( 'id', array('type' => 'hidden') );
echo $form->input( 'title' );
echo $form->input( 'nicename' );
echo $form->end( 'Save' );

However, when I visit the Edit screen, the input fields are NOT GETTING POPULATED with the requested data.

I'm using HTML Helper to generate the links and my edit link takes the form of:

http://localhost/contents/edit/id:4c8efaa0-02fc-46bf-ac65-06a07614f57d

As you can see, I've introduced two Debugger dumps in the controller's edit function... those are showing that $this->Content->id remains NULL when I visit this url.

BUT, if I modify the URL to this form (notice that I've used '=' instead of a ':'):

http://localhost/contents/edit/id=4c8efaa0-02fc-46bf-ac65-06a07614f57d

Debugger reports $this->Content->id to have the correct value...

However, my fields still don't get populated.

The index function is working fine though...

function index() {
    $this->set( 'contents', $this->Content->find( 'all' ) );
}

The above function is listing the contents correctly!

Where am I going wrong? Am I missing out on some essential bit of code in the View ? Any help will be much appreciated.

Thanks, m^e

+2  A: 

The problem is here:

http://localhost/contents/edit/id:4c8efaa0-02fc-46bf-ac65-06a07614f57d

Basically your url should look like:

http://localhost/contents/edit/4c8efaa0-02fc-46bf-ac65-06a07614f57d

As you may notice the id: is missing from the correct url. The reason: If you pass the id: this is named parameter and it's not assigned to $id in the function.

Your link for the edit form should look like this:

$this->Html->link('Edit', array('controller'=>contents', 'action'=>'edit', $id_variable));
Nik
Thank you Nik. While waiting for the reply, I decided to scratch the code altogether and start afresh... and I arrived at the same conclusion as yours by checking out the links generated by scaffolding.
miCRoSCoPiC_eaRthLinG
Ummm.. still got problems with the link generation.. Here's my code: echo $html->link( 'Edit', array( 'controller' => 'contents', 'action' => 'edit', 'id' => $item['Content']['id']) );
miCRoSCoPiC_eaRthLinG
And... never mind.. figured it out. It should be: echo $html->link( 'Edit', array( 'controller' => 'contents', 'action' => 'edit', $item['Content']['id']) )
miCRoSCoPiC_eaRthLinG

related questions