我有一个简单的博客,其中包含 Post 资源和 Comment 嵌套资源。到目前为止,我可以看到属于帖子的所有评论并为帖子创建新评论。
我想提供删除特定评论的可能性,但不知何故我犯了一些错误。
这是comments.index
包含所有评论的视图:
@extends('master')
@section('blog')
@foreach($comments as $comment)
<div class="span11 well">
<ul>
<li><strong>Body: </strong> {{ $comment->body }} </li>
<li><strong>Author: </strong> {{ $comment->author }}</li>
</ul>
{{ Form::open(array('method' => 'DELETE', 'route' => array('posts.comments.destroy', $post_id), $comment->id)) }}
{{ Form::submit('Delete', array('class' => 'btn btn-danger')) }}
{{ Form::close() }}
</div>
@endforeach
{{ link_to_route('posts.index', 'Back to Post index') }}
这是我运行索引时遇到的错误:路由“posts.comments.destroy”的参数“comments”必须匹配“[^/]++”(“”给定)以生成相应的 URL。
这是 CommentsController 中的 Index 方法:
public function index($post_id)
{
$comments = Post::find($post_id)->comments;
return View::make('comments.index', compact('comments'))->with('post_id', $post_id);
}
这是 CommentsController 中的 Destroy 方法:
public function destroy($post_id, $comment_id)
{
$comment = $this->comment->find($comment_id)->delete();
return Redirect::route('posts.comments.index', $post_id);
}
有人可以告诉我我在哪里犯了错误?
这是路线:
Route::resource('posts', 'PostsController');
Route::resource('posts.comments', 'CommentsController');