なんらかのコンテンツを追加する画面add.ctpには
POSTモデルに対してコンテンツを登録できるformがあります。
また同画面のナビバーには検索ボックスが用意されており、POSTモデルに対して
検索を実行できる機能があります。
それぞれPostsControllerでは
・コンテンツ登録→add
・検索→result
としてアクションを呼び出す想定です。
しかしコンテンツ登録をしようとするとaddではなく検索のresultアクションが呼び出されます。
同一モデルに対してアクションを分ける際は何かの対応が必要なのでしょうか。
一部ソースコードを掲載します。
----------------------<default.ctp>----------------------
----------------------<add.ctp>----------------------
<div class = "container"> <div class = "row"> <h2 class = "col-md-8 col-md-offset-2">投稿</h2> <div class = "col-md-8 col-md-offset-2"> <?php echo $this->Form->create('Post', array( 'inputDefaults' => array( 'div' => 'form-group', 'label' => array( 'class' => 'col col-md-3 control-label' ), 'wrapInput' => 'col col-md-9', 'class' => 'form-control' ), 'class' => 'well form-horizontal' )); ?> <?php echo $this->Form->hidden('user_id', array('value'=>$user_info['id']));?> <?php echo $this->Form->input('title', array( 'placeholder' => 'タイトル' )); ?> <?php echo $this->Form->input('comment', array( 'placeholder' => 'コメント', 'rows'=>3 )); ?> <div class="form-group"> <?php echo $this->Form->submit('投稿', array( 'div' => 'col col-md-9 col-md-offset-3', 'class' => 'btn btn-primary' )); ?> </div> <?php echo $this->Form->end(); ?> </div> </div> </div>----------------------<PostsController>----------------------
<?php class PostsController extends AppController { public $helpers = array('Html', 'Form'); public $components = array('Search.Prg'); public function beforeFilter(){ parent::beforeFilter(); //ユーザー情報をセット $this->set('user_info', $this->Auth->user()); $this->Auth->allow('index', 'view'); } public function result() { // 検索条件設定 $this->Prg->commonProcess(); if (isset($this->passedArgs['search_word'])) { //条件を生成 $conditions = $this->Post->parseCriteria($this->passedArgs); $this->paginate = array( 'conditions' => $conditions, 'limit' => 3, 'order' => array( 'id' => 'desc' ) ); $this->set('posts', $this->paginate('Post')); } } public function add() { if ($this->request->is('post')) { if ($this->Post->save($this->request->data)) { $this->Session->setFlash('Success!'); $this->redirect(array('action'=>'index')); } else { $this->Session->setFlash('failed!'); } } }回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。