【问题标题】:Laravel4: delete a commentLaravel4:删除评论
【发布时间】:2013-08-16 06:59:28
【问题描述】:

我有一个简单的博客,其中包含帖子资源和评论嵌套资源。 到目前为止,我可以查看属于某个帖子的所有 cmets 并为该帖子创建新评论。

我想提供删除特定评论的可能性,但不知何故我犯了一些错误。

这是 comments.index 与所有 cmets 的视图:

@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.cmets.destroy”的参数“cmets”必须匹配“[^/]++”(“”给定)以生成相应的 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');

【问题讨论】:

    标签: php laravel laravel-4 nested-resources


    【解决方案1】:

    您已在路线上放置了一个正则表达式测试器,以检查您的 comments 参数。
    此错误消息表明您提供给 Laravel 的参数不好。

    如果您的参数只是一个十进制 id,请改用 \d+ 正则表达式。

    【讨论】:

    • 对不起,alex,你能写出我要修改的代码行吗?
    【解决方案2】:

    如果没有您的 routes.php 文件 - 我不能确定,但​​我认为这可能是问题所在。

    改变

    {{ Form::open(array('method' => 'DELETE', 'route' => array('post.comments.destroy', $post_id), $comment->id)) }
    

    {{ Form::open(array('method' => 'DELETE', 'route' => array('post.comments.destroy', array ($post_id, $comment->id))) }
    

    如果这不起作用 - 请发布您的 routes.php 文件。

    编辑:您已将路线定义为“资源”。这意味着您的销毁路线仅使用一个变量定义。您实际上不需要包含 $post,因此只需定义以下内容:

    {{ Form::open(array('method' => 'DELETE', 'route' => array('posts.comments.destroy', $comment->id))) }}
    

    并将您的销毁方法更改为此 - $post 无需删除 $comment:

    public function destroy($comment_id)
    {
        $comment = $this->comment->find($comment_id)->delete();
    
        return Redirect::back();
    }
    

    【讨论】:

    • 我改了。现在我收到此错误:无法为命名路由“post.cmets.destroy”生成 URL,因为这样的路由不存在。我编辑问题,把路线放在那里
    • 对不起,这是一个拼写错误,我将帖子修改为帖子:{{ Form::open(array('method' => 'DELETE', 'route' => array('posts .cmets.destroy', array ($post_id, $comment->id))) } 现在的错误就像我之前得到的一样:路由“posts.cmets.destroy”的参数“cmets”必须匹配“[^/ ]++" ("" given) 生成对应的URL。
    • 我解决了!现在打开的表单是: {{ Form::open(array('method' => 'DELETE', 'route' => array('posts.cmets.destroy', $post_id, $comment->id))) }}
    • 完成评论的最后一件事:如何创建一个链接来显示一条评论。我尝试这样做: {{ link_to_route('posts.cmets.show', array($post_id, $comment->id)) }} 但是给出这个错误:缺少一些强制参数(“posts”,“cmets”)为路由“posts.cmets.show”生成一个 URL。
    猜你喜欢
    • 1970-01-01
    • 2021-05-01
    • 2011-12-18
    • 2010-12-12
    • 2015-01-04
    • 2023-03-24
    • 2012-03-09
    • 2020-07-12
    • 1970-01-01
    相关资源
    最近更新 更多