Layout not applied on page load

I have a different layout that I want to apply to my blogs. So in my blog controller I put

layout: 'blog'

      

at the top of my blogging controller above the index action.

When I physically refresh the page, the blog layout is applied. But if I link to blog_path from the master page the layout is not applied, it still uses the same layout as on the master page. Again, when I manually refresh the page, it applies the layout. Any suggestions?

+3


source to share


3 answers


Turbolinks

The problem will be Turbolinks

Turbolinks is a javascript library that works by taking your links, checking if the overall "page" changes, and if not, just pull the tag <body>

on the requested resource. Although painful, it is designed to be effective:

Turbolinks executes the following links faster in your web application. Rather than letting the browser recompile JavaScript and CSS between each page change, it keeps the current instance of the page alive and only replaces the body and heading in the head. Think CGI vs an ongoing process.

-

The main problem with Turbolinks is that it often just reloads <body>

your page tag , which is the problem you see here.

In most cases, this will break the JS / CSS present on the page; however, it seems to you that the problem is the ability to load layout

from your other activities.




Fix

The fix for this is more of a test (I've never used Turbolinks with layout

specific problems before)

I would recommend doing this:

#app/assets/javascripts/application.js
//require turbolinks -> REMOVE THIS LINE

#app/views/layouts/application.html.erb
<head>
  ...
  <%= stylesheet_link_tag "application" %> #-> remove turbolinks references
  <%= javascript_include_tag "application" %> #-> remove turbolinks references
</head>

      

This will basically remove any references to Turbolinks, allowing you to check if this is indeed the problem. I know this is because the layout applied when the page is refreshed is the hallmark of a Turbolinks problem (Turbolinks cannot work if you refresh the page); it's just a matter of how to work with it

+2


source


In the layout folder, if you define blog.html.erb, it will load it into BlogController.



0


source


you should have a layout file app/views/layout/blog.html.erb

.

0


source







All Articles