2011-05-16 4 views
0

@current_day와 @has_day를 내보기에 전달하는 방법을 궁금해하고 있었습니까?howto가 before_filter에서보기를 위해 vars를 전달합니다.

class PostController < ApplicationController 
    before_filter :detect_current_day 

    def index 
    #some logic 
    end 

private 

def detect_current_day 
    if (params[:year] && params[:month] && params[:day]) 
     @current_day = Date.parse params.values_at(:year, :month, :day).join('.') 
    else 
     @current_day = Date.today; 
    end 

    # Check if day already exist 
    @hasToday = Post.created_on(@current_day).count > 0 ? true : false; 
    end 

end 

감사

+0

인스턴스 변수를 설정하면 충분합니다. 그 이상이면 안됩니다. 너의 잘못은 뭐니? – apneadiving

+0

어딘가에 오타가 있음에 유의하십시오. 귀하의 질문에'@ has_day'가 언급되어 있지만 코드에는'@ hasToday'가 표시됩니다. – mbreining

답변

1

당신은 제대로했지만 당신은 잘못된 인스턴스 변수를 기대하고있다. 당신은 @current_day와 @hasToday를 정의했습니다. 당신은 당신이 has_day.please @ 원하는 view.if를 얻을 귀하의 전 필터 초기화합니다

@has_day = Post.created_on(@current_day).count > 0 ? true : false; 
0

난 당신의 코드와 아무 잘못 표시되지 않습니다,하지만 @current_day 날짜 형식이며, 당신이 원하는 경우 보기에서 인쇄하려면

<%= @current_day.strftime("%Y %m %d") %> 
관련 문제