2017-04-16 1 views
0

2.4.1 가이드가 사용LINK_TO '파괴', [comment.article, 주석, 레일 - 5.0.2 루비와 <a href="http://guides.rubyonrails.org/getting_started.html" rel="nofollow noreferrer">http://guides.rubyonrails.org/getting_started.html</a>에서

내가 방법을 배우고 싶은

[comment.article, comment] 

그것을 사용하려면,이게 무슨 의미. 저는 이해가 안되며 어떤 문서도 찾을 수 없습니다.

<%= link_to 'Destroy Comment', [comment.article, comment], 
      method: :delete, 
      data: { confirm: 'Are you sure?' } %> 

대신 article_comment_path (주석 포함 작동되지

<%= link_to 'Destroy Comment', article_comment_path, 
      method: :delete, 
      data: { confirm: 'Are you sure?' } %> 

() 오류가 표시 :

여기
ActiveRecord::RecordNotFound in CommentsController#destroy 

<%= link_to 'Destroy Comment', article_comment_path(comment) 

루트

Prefix Verb URI Pattern          Controller#Action 
    article_comments GET /articles/:article_id/comments(.:format)   comments#index 
        POST /articles/:article_id/comments(.:format)   comments#create 
new_article_comment GET /articles/:article_id/comments/new(.:format)  comments#new 
edit_article_comment GET /articles/:article_id/comments/:id/edit(.:format) comments#edit 
    article_comment GET /articles/:article_id/comments/:id(.:format)  comments#show 
        PATCH /articles/:article_id/comments/:id(.:format)  comments#update 
        PUT /articles/:article_id/comments/:id(.:format)  comments#update 
        DELETE /articles/:article_id/comments/:id(.:format)  comments#destroy 
      articles GET /articles(.:format)        articles#index 
        POST /articles(.:format)        articles#create 
     new_article GET /articles/new(.:format)       articles#new 
     edit_article GET /articles/:id/edit(.:format)      articles#edit 
      article GET /articles/:id(.:format)       articles#show 
        PATCH /articles/:id(.:format)       articles#update 
        PUT /articles/:id(.:format)       articles#update 
        DELETE /articles/:id(.:format)       articles#destroy 
       root GET /            articles#index 

답변

0

그것은 N에 대한 짧다을 Rails에서 ested route. 이와 같은 배열에 link_to를 제공하면/articles/1/comments/2와 같은 URL로 변환됩니다. 그들은 (더 명시 적있어 이후 대부분은 그러나 경로 도우미를 사용하는 예. article_comment_path(article_id: @article.id, id: @comment.id)

포스트 컨트롤러 코드를 파괴하는 방법뿐만 아니라 당신이 올바른 경로 설정이 있기 때문에 당신이 얻고있는 오류에 대해.

+0

article_comment_path (comment)를 사용하면 오류가 발생합니다. CommentsController # destroy ActiveRecord :: RecordNotFound –

+1

내 질문은 가이드의 [comment.article, comment]의 의미입니다. –

+0

나는 같은 질문을했고 당신의 질문을 보았습니다. 그러나 나는 그것을 알아 냈다. [comment.article, comment]는 중첩 된 경로가있는 링크가있을 때 사용할 수있는 Rails의 약자입니다. 그래서 그냥 편의. 중첩 된 경로를 사용하려면 부모 레코드의 id (comment.article과 함께있는 article id)와 자식 레코드 (comment id)가 필요합니다. 더 명시 적으로하려면 경로를 포함하고 괄호 안에 매개 변수를 대괄호로 묶지 마십시오 (예 : article_comment_path (comment.article, comment)). 그리고 실제로 위의 대답에서 경로를 사용하십시오. –

0

내가 무엇을 볼 수에서 경우 표시 <퍼센트 = [comment.article, 코멘트] %> 이 표시

[#<Article id: 1, title: "Title of next", text: "sdfsf", created_at: "2017-04-19 01:43:39", updated_at: "2017-04-19 01:43:39">, #<Comment id: 15, commenter: "sdfdsf", body: "sfsdfsd", article_id: 1, created_at: "2017-04-19 02:50:39", updated_at: "2017-04-19 02:50:39">] 

나는 레일-5.0.2가에 의해 할 각 ID 을 선택하고 선택하는 것을 알고 있다고 가정한다 파괴의 적절한 길.

정말 더 나은 논리를 얻을 수 없습니다.

좋아 돌아 가기 루트

<td><%= link_to 'Destroy', article_comment_path(@article,comment), method: :delete, data: { confirm: 'Are you sure?' } %></td> 

음이 한 일 완벽한에서 가이드 라인을 사용! 그래서 나는 이걸 붙잡는다.

관련 문제