Nikul
Nikul

Reputation: 1025

Doesn't get success message in Magento

I have controller called IndexController.php. I am getting message when i delete record in deleteAction().But i doesn't get any message when i add or edit record in addcategoryAction.Can anyone please look at in my controller and check why i doesn't get any message in addcategoryAction. Here is my controller.

<?php
class Company_Web_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction()
    {    
        $this->loadLayout();     
        $this->renderLayout();
    }

    public function addcategoryAction()
    {
        if ($this->getRequest()->isPost()) 
        {
            $data = $this->getRequest()->getParams();
            $id = $data['editid'];
            $catName = $data['catName'];
            $status = $data['status'];
            $data = array('name'=>$catName,'status'=>$status);
            $model = Mage::getModel('web/web');
            if($id == '')
            {
                $model->setData($data);
                try {
                        $insertId = $model->save()->getId();
                } catch (Exception $e){
                 echo $e->getMessage();
                }
                Mage::getSingleton('core/session')->addSuccess($this->__('Category has been successfully Added'));
            }
            else
            {
                $model->load($id)->addData($data);
                try {
                        $model->setId($id)->save();
                } catch (Exception $e){
                    echo $e->getMessage();
                }
                Mage::getSingleton('core/session')->addSuccess($this->__('Category has been successfully Updated'));
            }
            $this->_redirect('web/index/category');

        }
        $this->loadLayout();     
        $this->renderLayout();
    }

    public function categoryAction()
    {
        $this->loadLayout();
        $this->renderLayout();
    }

    public function deleteAction()
    {
        $id =  $this->getRequest()->getParam('id');
        $model = Mage::getModel('web/web');
        $model->setId($id)->delete();
        Mage::getSingleton('core/session')->addSuccess($this->__('Category has been successfully Deleted'));
        $this->_redirect('web/index/category');
    }
}

?>

Upvotes: 2

Views: 1020

Answers (1)

Amit Bera
Amit Bera

Reputation: 7611

Add the below code in controller action

$this->_initLayoutMessages('customer/session');
  $this->_initLayoutMessages('core/session');

Before render the layout using below code

$this->renderLayout();

and your below code in phtml file <div id="messages_product_view"><?php echo $this->getMessagesBlock()->getGroupedHtml() ?></div> According to addcategoryAction it is redirecting to categoryAction after model save

SO.modifed categoryAction is

 public function categoryAction()
    {
        $this->loadLayout();
        $this->_initLayoutMessages('customer/session');
     $this->_initLayoutMessages('core/session');
    $this->renderLayout();
}

And add <div id="messages_product_view"><?php echo $this->getMessagesBlock()->getGroupedHtml() ?></div> in phtml file of categoryAction

Upvotes: 2

Related Questions