0

I have a Horse object that has a completed variable (text). I want to be able to click set complete in the View and then have that horse's completed variable set to "yes." After this, we would redirect the user to tasks/index. My thinking is to create setcomplete($id) function in Controller, so I could pass /setcomplete/4 and that horse's flag would change.

I have created a basic view for it, but the idea is that I would not need a view, but I do not know how to get around that....I was thinking of using $this->render() or something of this nature.

This is the basic code...pass in the id and then change the variable, then redirect to tasks/index.

I am not getting an error....its just not working, that's all.

public function setcomplete($id = null) {
        if (!$this->Task->exists($id)) {
            throw new NotFoundException(__('Invalid task'));
        }
        if ($this->request->is(array('post', 'put'))) {
            $this->set('completed', 'yes');
            if ($this->Task->save($this->request->data)) {
                $this->Session->setFlash(__('The task has been updated.'));
                return $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The task could not be saved. Please, try again.'));
            }
        } else {

        }

    }
3
  • What behavior are you seeing? However $this->layout = $this->autoRender = false;, will stop it from throwing errors about a missing view. Commented Nov 14, 2013 at 23:59
  • im seeing no behavior at all...i set up a very basic view and it goes there but it doesnt change the variable, which is what I want it to do. Commented Nov 15, 2013 at 1:09
  • Can you add debug($this->request->data) above the line that does the save and post the results here? Might help pinpoint the issue.. Commented Nov 15, 2013 at 7:05

1 Answer 1

1

According to my understanding of your problem. you want to set the completed to yes for Task model. i think you've done in a wrong way here.

what about the $this->request->data here? 

Does it contain the data you want to save for the task model like the structure as below

Array(
    'Task' => Array(
        'id' => 4, // for eg..
        'completed' => 'yes'
    )
)

if not

you can solve the problem by doing like this. [Assuming you don't have $this->request->data]

public function setcomplete($id = null) {
    if (!$this->Task->exists($id)) {
        throw new NotFoundException(__('Invalid task'));
    }

    $this->Task->id = $id;
    $this->Task->set('completed', 'yes')

    if ($this->Task->save()) {
        $this->Session->setFlash(__('The task has been updated.'));
        return $this->redirect(array('action' => 'index'));
    } else {
        $this->Session->setFlash(__('The task could not be saved. Please, try again.'));
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.