Wednesday, March 30, 2011

form, url, and id

Using 1.3.7

I'm having some trouble with routes again. Specifically, routes and FormHelper.

Router::connect(
'/admin/galleries/:gallery_id/edit_image/:image_id',
array(
'admin' => 1,
'controller' => 'galleries',
'action' => 'edit_image'
),
array(
'gallery_id' => '[0-9]+',
'image_id' => '[0-9]+',
'pass' => array('gallery_id', 'image_id')
)
);

echo $this->Html->link(
'edit',
array(
'admin' => 1,
'controller' => 'galleries',
'action' => 'edit_image',
'gallery_id' => $data['Gallery']['id'],
'image_id' => $image['id']
),
array('title' => 'edit this image')
);

This works peachy. The problem is with the edit form (Gallery.id = 1,
Image.id =42):

echo $this->Form->create(
'Image',
array(
'url' => array(
'admin' => 1,
'controller' => 'galleries',
'action' => 'edit_image',
'gallery_id' => $this->data['Gallery']['id'],
'image_id' => $this->data['Image']['id']
),
'type' => 'file'
)
);

Result:
/admin/galleries/edit_image/42/gallery_id:1/image_id:42

Similarly:

Router::connect(
'/admin/galleries/:gallery_id/delete_image/:image_id',
array(
'admin' => 1,
'controller' => 'galleries',
'action' => 'delete_image'
),
array(
'gallery_id' => '[0-9]+',
'image_id' => '[0-9]+',
'pass' => array('gallery_id', 'image_id')
)
);

echo $form->create(
'Gallery',
array(
'url' => array(
'admin' => 1,
'controller' => 'galleries',
'action' => 'delete_image',
'gallery_id' => $this->data['Gallery']['id'],
'image_id' => $this->data['Image']['id']
)
)
);

Result:
/admin/galleries/delete_image/1/gallery_id:1/image_id:42


OK -- now try ...

Router::connect(
'/admin/galleries/:id/delete_image/:image_id',
array(
'admin' => 1,
'controller' => 'galleries',
'action' => 'delete_image'
),
array(
'id' => '[0-9]+',
'image_id' => '[0-9]+',
'pass' => array('id', 'image_id')
)
);

echo $form->create(
'Gallery',
array(
'url' => array(
'admin' => 1,
'controller' => 'galleries',
'action' => 'delete_image',
'image_id' => $this->data['Image']['id']
)
)
);

Result:
/admin/galleries/delete_image/1/image_id:42

alright ...

echo $form->create(
'Gallery',
array(
'url' => array(
'admin' => 1,
'controller' => 'galleries',
'action' => 'delete_image',
'id' => $gallery_id,
'image_id' => $this->data['Image']['id']
)
)
);

Result:
/admin/galleries/delete_image/1/id:1/image_id:42

This game is rigged!

As I understand it, if $this->data[ModelUsedForForm]['id'] is set, it
gets shoved into the URL *even if the URL is being explicitly
specified*. Mark Story's comment to this ticket [1] suggests that this
was fixed in 1.3. Anyone know the scoop on this?

FWIW, I'm not editing/deleting the image in ImagesController because I
have 5 models which can be associated with Image and they all have
different requirements. However, I may now rethink this strategy and
put up with lots of switch()es in ImagesController.

[1] http://cakephp.lighthouseapp.com/projects/42648/tickets/249-form-create-alters-specified-url

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: