2013-08-16 6 views
0

게시물 리소스 및 댓글 중첩 리소스가있는 간단한 블로그가 있습니다. 지금까지 게시물에 속한 모든 댓글을보고 게시물에 대한 새 댓글을 만들 수 있습니다.Laravel4 : 댓글 삭제

특정 의견을 삭제할 수있는 가능성을 부여하고 싶지만 어쨌든 몇 가지 실수를 저지르고 있습니다. ^/매개 변수 "의견"[ "와 일치해야합니다 경로"posts.comments.destroy "에 대한 :

@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') }} 

이 내가 인덱스를 실행 얻을 오류는 다음과 같습니다

모든 의견보기 comments.index이다 ] ++ "(" "주어진 URL을 생성한다.

이것은 CommentsController 내부의 색인 방법 :

public function index($post_id) 
{ 
    $comments = Post::find($post_id)->comments; 
    return View::make('comments.index', compact('comments'))->with('post_id', $post_id); 
} 

그리고 이것이다 CommentsController 내부의 파괴 방법 : 내가 실수를하고 어디

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'); 

답변

2

, 당신의 comments 매개 변수를 확인합니다.
이 오류 메시지는 Laravel에 제공하는 매개 변수가 좋지 않음을 나타냅니다.

매개 변수가 십진수 ID 인 경우 \d+ regexp를 대신 사용하십시오.

+0

죄송가 알렉스, 수정해야 할 코드 줄을 정확히 쓸 수 있습니까? – johnnyfittizio

0

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 파일을 게시하시기 바랍니다.

편집 : 경로를 "자원"으로 정의했습니다. 즉, 파괴 경로가 하나의 변수만으로 정의됩니다. 당신은 그래서 그냥이 정의에 포함 된 $ 포스트를 필요 실제로 그나마이에 방법을 파괴

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

을하고 변경 - 달러 (A $) 주석 삭제하는 $의 게시물에 대한 필요가 없습니다 :

public function destroy($comment_id) 
{ 
    $comment = $this->comment->find($comment_id)->delete(); 

    return Redirect::back(); 
} 
+0

그것을 바꿉니다. 이제이 오류가 발생합니다 : "post.comments.destroy"라는 경로가 존재하지 않으므로 해당 경로의 URL을 생성 할 수 없습니다. 경로를 두는 질문을 편집합니다 – johnnyfittizio

+0

죄송합니다. 실수로 입력 한 것으로 게시물을 수정합니다 : {{Form :: open (array ('method'=> 'DELETE', 'route'= > 배열 ('posts.comments.'파괴 배열 ($ post_id를, $ 대하여 의견> 아이디)))} 이제 오류가 내가 전에 가지고 것과 같은 것입니다 : "[일치해야합니다 경로"posts.comments.destroy "에 대한 매개 변수"의견 "^/] ++ "(" "주어진 URL을 생성한다. – johnnyfittizio

+0

해결되었습니다! 폼 개구 지금 : {{형태 : 개방 (배열 ('방법'=> '삭제', '행'=> 어레이 ('posts.comments.destroy'$ post_id를, $ 대하여 의견> ID)))}} – johnnyfittizio