2016-08-29 5 views
0

application.html.erb 파일과보기 파일이 결합 된 방법에 대해 매우 혼란 스럽습니다.본문 내용을 본문 섹션으로 옮기는 이유는 무엇입니까?

<html> 
<head> 
    <!-- View Specific Stylesheet --> 
    <link href="/css/ViewSpecific.css" rel="stylesheet" /> 
</head> 
<body> 
    <!-- View Specific Body --> 
</body> 

내가 기대하는 이들 두 파일이 결합 할 때,보기 특정 : 내보기 파일에서

<!DOCTYPE html> 
<html> 
    <head> 
    <!-- Bootstrap Css --> 
    <link href="/bootstrap-assets/css/bootstrap.css" rel="stylesheet"> 
    <%= yield :head %> 
    </head> 
<body> 
    <nav> 
     <!--Long navbar section --> 
    </nav> 
    <%= yield %> 
</body> 
</html> 

: 내 application.html.erb 파일 다음 (짧음을 최소화 내용)에이 스타일 시트로드는 <head> 섹션에 표시되고 특정 바디로드는 <body> 섹션에 표시됩니다. 최종 결과는 navbar (application.html.erb)이 이미로드 된 후보기 특정 스타일 시트를 실제로 <body> 섹션으로 보냅니다. 이것은 분명히 스타일 시트가로드되기 전에 내 몸의 일부가로드되는 원치 않는 결과를 가져오고, 사용자가 처음 보는 모든 것은 페이지를 모두 끔찍하게 보입니다.

답변

1

application.html.erb의 헤드 섹션에 특정 뷰 head을 렌더링하려면 content_for을 사용해야합니다. 당신이 application.html.erb:head를 얻을 수 있기 때문에

<% content_for :head do %> 
<!-- View specific head section --> 
    <link href="/css/ViewSpecific.css" rel="stylesheet" /> 
<% end %> 

를 사용하여, 모든 수행 할 수 이잖아. 그리고 뷰에 html 태그가 필요하지 않습니다.

자세한 내용은 http://apidock.com/rails/v4.2.1/ActionView/Helpers/CaptureHelper/content_for

관련 문제