2010-04-12 4 views
9

나는 항상 레일스와 프로그래밍에 익숙하지 않으므로 쉽게 입력하십시오. 미리 감사드립니다.레일즈에 블로그 루비 만들기 - 댓글 삭제 문제

저는 Ryan Bates의 초기 자습서를 how to build a weblog in 15 minutes에 성공적으로 따라 왔습니다. 이 튜토리얼을 모르는 경우 게시물 작성을 통해 해당 게시물에 대한 주석을 허용합니다. 또한 show.html.erb 게시물에 대한 주석을 작성하고 표시하여 AJAX를 소개합니다. 모든 작품은 훌륭합니다.

다음은 딸꾹질입니다. Ryan이이 튜토리얼을 사용하면 comments_controller를 지우고 댓글 작성 코드 만 보여줍니다. 나는 코멘트를 편집하고 파기하는 능력을 다시 추가하려고 노력하고있다. 작동시키지 못하는 것 같아서, 실제 게시물을 삭제하지 않고 주석 (로그는 PostsController에 DELETE 요청을 계속 보낸다는 것을 보여줍니다).

class CommentsController < ApplicationController 
def create 
    @post = Post.find(params[:post_id]) 
    @comment = @post.comments.create!(params[:comment]) 
    respond_to do |format| 
    format.html { redirect_to @post } 
    format.js 
    end 
end 

def destroy 
    @comment = Comment.find(params[:id]) 
    @comment.destroy 

    respond_to do |format| 
     format.html { redirect_to(posts_url) } 
     format.xml { head :ok } 
    end 
    end 
end 

/views/posts/show.html.erb

<%= render :partial => @post %> 

    <p> 
     <%= link_to 'Edit', edit_post_path (@post) %> | 
     <%= link_to 'Destroy', @post, :method => :delete, :confirm => "Are you sure?" %> | 
     <%= link_to 'See All Posts', posts_path %> 
    </p> 

    <h2>Comments</h2> 
    <div id="comments"> 
     <%= render :partial => @post.comments %> 
    </div> 

    <% remote_form_for [@post, Comment.new] do |f| %> 
     <p> 
      <%= f.label :body, "New Comment" %><br/> 
      <%= f.text_area :body %> 
     </p> 
     <p> 

<%= f.submit "Add Comment" %></p> 
<% end %> 

/views/comments/_comment.html.erb

<% div_for comment do %> 
    <p> 
     <strong>Posted <%= time_ago_in_words(comment.created_at) %> ago 
     </strong><br/> 
     <%= h(comment.body) %><br/> 
     <%= link_to 'Destroy', @comments, :method => :delete, :confirm => "Are you sure?" %> 
    </p> 
<% end %> 

routes.rb

: 여기 내 코드입니다
ActionController::Routing::Routes.draw do |map| 
    map.resources :posts, :has_many => :comments 
    map.connect ':controller/:action/:id' 
    map.connect ':controller/:action/:id.:format' 
end 
+0

"제대로 작동하지 않습니다"- 실제로 문제를 해결하기 위해 무엇을 했습니까? – fig

+0

comment.html.erb의 link_to를 @comment, comment, comments 등으로 변경해 보았습니다. CommentsController를 사용하여 @comment = Comment.find (params [: id]) @ post.comment.destroy (params [: comment_id])를 포함합니다. 몇 가지 다른 사람도 있지만 그것은 routes.rb 파일에 righth 또는 comments.html.erb의 link_to에 뭔가가없는 것 같습니다. 노선 코드를 포함하도록 업데이트하겠습니다. – bgadoci

답변

12

meagar은 옳은 길을하지만,이 중첩 된 경로이기 때문에 당신은 할 필요가 : 기반 경로를 알아낼

그래서 <%= link_to 'Destroy', [@post, comment], ... %>

, 당신은 의견과 게시물을 전달하고 레일을시키는 있습니다 당신 정의에.

+0

감사합니다. 이 모든 조합을 시도한 것처럼 보입니다. 완벽하게 일했습니다. – bgadoci

1

_comments.html.erb에서,210 IE

<%= link_to 'Destroy', comment, ... %> 

에, 그것을 comment 필요 없어 전체 @comments 배열 자체를 통과한다.

+0

내가 할 때이 오류가 발생합니다 : # 에 대한 정의되지 않은 메서드'comment_path ' – bgadoci

관련 문제