1
votes
php - Cakephp Blog Tutorial - Add Routine - Stack Overflow
Asked
Viewed 136 times
1

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?

    2

    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')) {

      0

      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');

        Your Answer

        By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

        Not the answer you're looking for? Browse other questions tagged or ask your own question.

         
        2

        2 Answers

        2
        votes

        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')) {

        0
        votes

        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');