yohannan_sobin
yohannan_sobin

Reputation: 917

Cake PHP Form Helper

I am facing a problem with Form Helper. My controller name is posts_controller.php and it is as below

<?php
    Class PostsController extends AppController
    {
        var $name='Posts';
        var $helpers=array('Html','Form','Link');
        var $components = array('Session');
        function index()
        {
            $this->pageTitle='Cake PHP Index page';            
            $this->paginate();
            $this->set('posts',$this->Post->find('all'));
        }

        function view($id=null)
        {
            $this->Post->id = $id;
            $this->set('post', $this->Post->read());
        }

        function add()
        {
            if(!empty ($this->data))
            {
                if($this->Post->save($this->data))
                {
                    $this->Session->setFlash('Your post has been saved.');
                    $this->redirect(array('action' => 'index'));
                }
            }
        }
    }
?>

When I go to the add action the view add.ctp is loaded with the corresponding form.

This is my view file add.ctp

<?php
    echo $this->Form->create('Post');
    echo $this->input('title');
    echo $this->input('body');
    echo $this->Form->end('Save');
?>

When I submit the form I am getting an error saying that posts action is not defined in your controller. And when I checked the source of the page in browser, the action of the form have a false value.The value I am getting is

<form id="PostAddForm" method="post" action="/cakephp/app/webroot/index.php/posts/posts/add">

instead of

<form id="PostAddForm" method="post" action="/cakephp/app/webroot/index.php/posts/add">

Can you help me?

Upvotes: 0

Views: 2159

Answers (4)

Sumit Kumar
Sumit Kumar

Reputation: 1902

I think this should be written like this

$this->Form->create("Post",array('action'=>'add'));

Upvotes: 0

tomelin5
tomelin5

Reputation: 55

Also you forgot a ' in your code:

 echo $this->input('title);
 echo $this->input('title');

Upvotes: 0

Leo
Leo

Reputation: 6571

Like Nik says, it looks like a mod_rewrite problem. That form HTML should look like:

<form id="PostAddForm" method="post" action="/YourSite/posts/add">

mod_rewrite is an Apache module. Usually it is not enabled by default. There are plenty of pages explaining how to do it, but if you're on Ubuntu or Debian it can be a bit of a mystery. If that is the case, look here: http://bit.ly/ubuntu_mod_rewrite

Upvotes: 1

Nik Chankov
Nik Chankov

Reputation: 6047

Try to enable mod_rewrite on your server. For me this is the problem, especially if your code is that what you posted.

Upvotes: 2

Related Questions