2016-09-30 2 views
0

컨트롤러 메서드에서 변수를 정의하고 있습니다. 변수의 크기가 0 인 경우 js를 렌더링하려고합니다.가변 길이가 0 인 경우 js 렌더링 (레일)

내 코드는 다음과 같습니다

def department 
    @department = Department.all 
    if @department.count > 0 
    render :html => html-template-name 
    else 
    render :js => "alert('No department available');" 
    end 
end 

이를 :

위의 방법은 department.html.erb라고
def department 
    @department = Department.all 
end 

지금 내가 원하는

렌더링 또는 같은 것을 할됩니다 count가 0보다 크면 html을 렌더링하고 그렇지 않으면 js를 렌더링해야합니다. render html part의 구문은 무엇이되어야합니까?

+0

당신이 사용할 필요 "템플릿을 렌더링", HTML 템플릿 대신 " –

+0

AJAX 요청을 하시겠습니까? html 호출? –

+0

정상적인 html로 전화 – vidal

답변

1

render html:은 템플릿을 렌더링하는 데 사용되지 않습니다. html을 렌더링하는 데 사용됩니다. 예 :

render html: "<strong>Not Found</strong>".html_safe 

난 당신이 render 또는 render template:

render "products/show" 

또는

render template: "products/show" 

문서를 찾고 있습니다 믿습니다 Using Render

관련 문제