Question

I followed the cakePHP Blog Tutorial and I have a logical error happened in the cakephp/posts/add Routine

http://book.cakephp.org/2.0/en/tutorials-and-examples/blog/part-two.html

I got the default cakephp/posts/add Routine from the tutorial working just fine, but when I duplicate the routine and try to rename it to cakephp/apples/add (Posts to Apples) it seems that $this->Apple->save($this->request->data) and $this->redirect(array('action' => 'index')) is not working it just refreshes me the page and doesn't redirect to the index view and doesn't save the record also.

public function add() {
    if ($this->request->is('apple')) {
        $this->Apple->create();
        if ($this->Apple->save($this->request->data)) {
            $this->Session->setFlash(__('Your post has been saved.'));
            return $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Unable to add your post.'));
    }
}   

what would be the possible problem in this?

Was it helpful?

Solution

The problem lies at this line:

if ($this->request->is('apple')) {

You are checking for the type of request to determine if it is a form submission (usually POST), hence it should instead be

if ($this->request->is('post')) {

OTHER TIPS

Ypu should handle request with $this->request->is('post'); or $this->request->is('get'); and if you want use different model in default controller like in PostsController want to use Apple model you need load this model in controller like $this->loadModel('Apple'); or define in $uses variable like public $uses = array('Post', 'Apple');

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top