2014-01-27 3 views
1

2 개의 객체 배열을 반복하는 방법을 정렬하여 뷰에 혼합 된 순서로 - 생성 날짜별로 표시하려고합니다. 뷰에서두 객체 배열을 반복하며 생성 된 날짜순 정렬

def browse 
    @user = current_user 
    if params[:category] 
    @category = Category.find(params[:category]) 
    @posts = @category.posts.page(params[:page]) 
    else 
    @posts = Post.search(params) 
    end 
end  

, 나는 단지 루프를 통해 출력이 같은 :

여기에만 검색 쿼리를 기반으로, 당신이있어 범주에 따라 게시물을 표시하거나, 내 현재의 컨트롤러 코드입니다 그래서 : - 객체를 가져옵니다 : (@everything 예) 내가 할

- if @posts 
    - @posts.each do |post| 
    = post.name 
    = post.content 

싶은 무엇, 대신 인스턴스 변수 @posts를 통해 게시물을 참조이다 ... 나는 새로운 변수를 생성하고 싶습니다 Post 클래스와 Comment 클래스에서 동일한 배열로 팝업하고, 루프를 반복합니다. GH 및 출력이처럼 내보기에 각각 각각 :

이상적인 컨트롤러 :

def browse 
    @user = current_user 
    if params[:category] 
    @category = Category.find(params[:category]) 
    @everything = @category.everything(params[:page]) # ... combination of comments and posts 
    else 
    @everything = Everything.search(params) 
    end 
end  

이상적인보기 : 어떤 도움/지침은 감사

- if @everything 
    - @everything.each do |e| 
    - if e.type == 'post' 
     = e.name 
     = e.content 
    - else 
     = e.comment 

. 나는이 문제에 어떻게 접근해야할지 모르겠다.

당신은 일의이 유형을 할 것

답변

2

%ul= render @everything 

A A보기/의견/_comment.html.haml 및이 있는지 확인보기에서

def browse 
    @user = current_user 
    @everything = @category.posts | @category.comments 
end 

을 (당신이 시작하는) 조회수/게시물 수/_post.html.haml 파일

또는 특정 부분을 렌더링하고있다

%ul= render :partial => shared/everything_item, :collection => @everthing 
의 차이를 처리 할 수