2013-12-12 2 views
0

게시물, 질문 및 의견 (질문에 포함 된 주석은 게시물에 속해 있음) 및 의견 색인 페이지에서 마지막 두 가지 질문을 표시하려고합니다. 여기 정의되지 않은 메서드 'questions'

<% @questions.each do |question| %> 
<%= question.body %> 
<% end %> 

내가 점점 오전 오류입니다 :

def index 

@question = Question.find params[:question_id] 
@comments = @question.comments 
@questions = @comment.questions.order(:created_at).limit(2).reverse_order 

end 

내 comments_index :
가 여기 내 comments_index 기능입니다

undefined method `questions' for nil:NilClass 

내 routes.rb 파일은 다음과 같습니다 :

resources :posts do 
    resources :questions do 
    end 
end 

resources :questions do 
    resources :comments do 
    end 
end 

답변

1

질문 has_many 코멘트를 각 주석의 마지막이 질문에 얻고 싶은 것 때문에 @comment

def index 
    @question = Question.find params[:question_id] 
    @comments = @question.comments 
    @questions = @comments.collect{|c|c.questions.order(:created_at).limit(2).reverse_order} 
end 

을 사용하고 있습니까? 댓글 소유 여부 질문?

그럴 경우 댓글의 1 개 질문 만 가져올 수 있지만 질문 페이지로 이동하면 이미 확인 중입니다 ... 마지막으로 질문 2 개 (기간) :

@post = @question.post 
@questions = @post.questions.order(:created_at).last(2) 

그러면 데이터베이스에서 마지막 두 가지 질문을 받게됩니다.

하고 경로

... 그것은 안 :

resources :posts do 
    resources :questions do 
    resources :comments do 
    end 
    end 
end 

?

+0

게시물에 속한 마지막 2 개의 질문이되기를 바란다. 미안하지만 내가 충분히 명확하지 않았다면. 네가 내 길에 대해 옳다고 생각해. 내가 바꿀거야. – user2759575

+0

so ...'@ question.post.questions.order (: created_at) .last (2)'. 보기에서 사용할 인스턴스 변수로'@ post '를 가져오고 싶을 수도 있습니다. 그래서 ...'@post = @ question.post'. @ post.questions 등 – Dudo

0

오타가 있습니다. 당신은 @comment의 초기화하지만 의견을 수집, 당신은

+0

음, 아무 것도 표시하지 않는 것 같습니다. – user2759575