2014-11-03 2 views
0

마이크로 포스트 밑에있는 주석을 작성하여 Michael Hartl의 Ruby on Rails 튜토리얼을 후속 처리하려고합니다. 나는 코멘트 모델을 만들었고, 주석을 belongs_to 사용자와 마이크로 포스트뿐만 아니라 마이크로 포스트 has_many 코멘트와 사용자 has_many 코멘트로 연관 지을 수 있습니다. 그러나 이것을 구현하는 방법이 궁금합니다.Ruby on Rails 중첩 된 설명을 만드는 방법

의견 양식을 마이크로 포스트 모델로 렌더링합니까?

누군가이 구조를 빠르게 구현할 수 있습니까? 인덱스 게시물과 댓글 : 뷰 파일에서 posts_controller.rb

class PostsController < ApplicationController 
    def index 
    // **get all posts and eager load their comments just in one database connection** 
    // if you also needs the owner get it same way here by eager loading. 
    // Do not write quires in the views. 
    @posts = Post.all.includes(:comments) 
    respond_to do |format| 
     format.html 
    end 
    end 
end 

에서

당신은 그래서 소요 그 부분 만들기 다른 곳에서 인덱스 의견려고하는 경우에

+0

중첩 된 의견의 의미를 명확히 할 수 있습니까? 댓글이 다른 댓글에 속할 수 있도록 답장 댓글을 달 수 있도록 하시겠습니까? 그래서 같을 것이다 : - 홈페이지 주석 1 이 - 주요 주석 1 1 응답 - 가 ' 홈페이지 의견이 - - 주요 주석 1 2 답글 3 ' – Rose

+0

은 현재 난 그냥 원하는 홈페이지 코멘트 코멘트 마이크로 포스트에 속한 – Jason

+0

질문하기 : http://stackoverflow.com/questions/22635981/nested-comments-from-scratch – cweston

답변

0

샘플 코드 주석 배열을 렌더링하거나 하나의 파일에서만 수행 할 수 있지만 첫 번째 파일은 훨씬 더 깨끗합니다.

코드는 HAML 스타일의 것과 같아야합니다 : 당신의 부분 _comment.html.haml ::

%p 
    = "Content ::" + comment.content 
%p 
    = "Owner name ::" + comment.owner.name 

또는 당신은 또한 주석을 통해 부분적인 루프를 만들 수있는

- @posts.each do |post| 
    // The post it self 
    %p 
    = post.content 
    // whatever data from the post 
    // loop through the post comments 
    - post.comments.each do |comment| 
    // Render the partial of one comment 
    // this partial should include whatever data or style for your form 

    // NOTE if your partial in same directory write 
    = render 'comment', comment: comment 

    // If it is under comments directory which is better so Write 
    = render 'comments/comment', comment: comment 

    // if you need anything from the post also pass it to the form. 

귀하의 게시물보기는 각 게시물마다 그것을 렌더링합니다

- @posts.each do |post| 
    = render 'comments', post: post 

부분

- post.comments.each do |comment| 
    %p 
    = "Content ::" + comment.content 

이것은 사용자가 요청한대로 수행 할 수있는 방법을 보여주기위한 코드 예제 일뿐입니다.

+0

아메드가 내 다른 게시물을 볼 수 있습니까? 나는 이것이 더 나은 사진을 줄 수 있었으면 좋겠다. – Jason

관련 문제