質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

CakePHP

CakePHPは、PHPで書かれたWebアプリケーション開発用のフレームワークです。 Ruby on Railsの考え方を多く取り入れており、Railsの高速性とPHPの機動性を兼ね備えています。 MVCやORMなどを「規約優先の考え方」で利用するため、コードを書く手間を省くことができます。 外部のライブラリに依存しないので、単体での利用が可能です。

Q&A

解決済

1回答

408閲覧

メインテーブルに紐付けたテーブルのアクションを行うと”viewがない”とエラーが出る

tunnel

総合スコア30

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

CakePHP

CakePHPは、PHPで書かれたWebアプリケーション開発用のフレームワークです。 Ruby on Railsの考え方を多く取り入れており、Railsの高速性とPHPの機動性を兼ね備えています。 MVCやORMなどを「規約優先の考え方」で利用するため、コードを書く手間を省くことができます。 外部のライブラリに依存しないので、単体での利用が可能です。

0グッド

0クリップ

投稿2019/06/20 08:58

編集2019/06/20 10:20

postsテーブルとcommentsテーブルを作り、postsの詳細画面でcommentのフォームと一覧が表示されるようにしたいのですが、フォームから投稿すると、
Error: The view forCommentsController::add() was not found.
というエラーになります。pathを見ると~/posts/view/1となってほしいのですが~/comments/addになってしまいます。
commentコントローラのadd()でviewにリダイレクトしているので原因がわかりません。よろしくお願いします。

php

1//view.ctp 2//postsの詳細ページ 3<?php 4$this->assign('title', 'Blog Ditail'); 5 ?> 6 7<h1> 8 <?= $this->Html->link('Back', ['action'=>'index'], ['class'=>['fs12', 'push-right']]); ?> 9 <?= h($post->title); ?> 10</h1> 11 12<p><?= nl2br(h($post->body)); ?></p> 13 14<?php if (count($post->comments)) : ?> 15<h2>Comments<span class="fs12">(<?= count($post->comments); ?>)</span></h2> 16 <ul> 17 <?php foreach ($post->comments as $comment) : ?> 18 <li> 19 <?= h($comment->body); ?> 20 </li> 21 <?php endforeach; ?> 22 </ul> 23<?php endif; ?> 24 25<h2>New comment</h2> 26<?= $this->Form->create(null,[ 27 'url'=>['controller'=>'Comments', 'action'=>'add'] 28]); 29?> 30<?= $this->Form->input('body', ['required' => true]) ?> 31<?= $this->Form->hidden('post_id', ['value'=>$post->id]); ?> 32<?= $this->Form->button('Add'); ?> 33<?= $this->Form->end(); ?>

php

1//PostsController.php 2<?php 3 4namespace App\Controller; 5 6class PostsController extends AppController 7{ 8 9 public function index() 10 { 11 $posts = $this->Posts->find('all'); 12 $this->set(compact('posts')); 13 } 14 15 public function view($id = null) 16 { 17 $post = $this->Posts->get($id, [ 18 'contain'=>'Comments' 19 ]); 20 $this->set(compact('post')); 21 } 22 23 public function add() 24 { 25 $post = $this->Posts->newEntity(); 26 if ($this->request->is('post')) { 27 $post = $this->Posts->patchEntity($post, $this->request->data); 28 if ($this->Posts->save($post)) { 29 $this->Flash->success('Add success!'); 30 return $this->redirect(['action'=>'index']); 31 } else { 32 $this->Flash->error('Add error!'); 33 } 34 } 35 $this->set(compact('post')); 36 } 37 38 public function edit($id = null) 39 { 40 $post = $this->Posts->get($id); 41 if ($this->request->is(['post', 'patch', 'put'])) { 42 $post = $this->Posts->patchEntity($post, $this->request->data); 43 if ($this->Posts->save($post)) { 44 $this->Flash->success('Edit success!'); 45 return $this->redirect(['action'=>'index']); 46 } else { 47 $this->Flash->error('Edit error!'); 48 } 49 } 50 $this->set(compact('post')); 51 } 52 53 public function delete($id = null) 54 { 55 $this->request->allowMethod(['post','delete']); 56 $post = $this->Posts->get($id); 57 if ($this->Posts->delete($post)) { 58 $this->Flash->success('Delete success!'); 59 } else { 60 $this->Flash->error('Delete error!'); 61 } 62 return $this->redirect(['action'=>'index']); 63 } 64}

php

1//CommentsController.php 2<?php 3 4namespace App\Controller; 5 6class CommentsController extends AppController 7{ 8 9 public function add() 10 { 11 $comment = $this->Comments->newEntity(); 12 if ($this->request->is('comment')) { 13 $comment = $this->Comments->patchEntity($comment, $this->request->data); 14 if ($this->Comments->save($comment)) { 15 $this->Flash->success('Comment Add success!'); 16 return $this->redirect(['controller'=>'Posts', 'action'=>'view', $comment->post_id]); 17 } else { 18 $this->Flash->error('Comment Add error!'); 19 } 20 } 21 $this->set(compact('comment')); 22 } 23 24 public function delete($id = null) 25 { 26 $this->request->allowMethod(['post','delete']); 27 $post = $this->Posts->get($id); 28 if ($this->Posts->delete($post)) { 29 $this->Flash->success('Delete success!'); 30 } else { 31 $this->Flash->error('Delete error!'); 32 } 33 return $this->redirect(['action'=>'index']); 34 } 35}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

commentsControllerのadd()のif ($this->request->is('comment')) が間違っていました。
'comment'→'post'でした。

投稿2019/06/21 06:51

tunnel

総合スコア30

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問