ruby on rails - Why is my view file moving all my head content into the body section? -


i'm confused how application.html.erb file , view files combine. have in application.html.erb file following (minimized content brevity):

<!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> 

in view file:

<html> <head>     <!-- view specific stylesheet -->     <link href="/css/viewspecific.css" rel="stylesheet" /> </head> <body>     <!-- view specific body --> </body> 

what expect when these 2 files combine, view specific stylesheet load in <head> section, , view specific body load in <body> section. final result sends view specific stylesheet <body> section, after navbar application.html.erb loaded. produces unwanted result part of body loading before stylsheet loaded, , first second user views page looks terrible.

you should make use of content_for if want render view-specific head head section of application.html.erb. use

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

since yield :head in application.html.erb, thats done. , don't need html tags in views.

for more, see http://apidock.com/rails/v4.2.1/actionview/helpers/capturehelper/content_for


Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -