Rails 成功之路Ruby、Rails知识Ruby & Rails

Using Gon and turbolinks in Rail

2016-05-26  本文已影响76人  老码农不上班

Include Gon in body instead of head when using turbolinks
The reason as follow:

When you follow a link, Turbolinks automatically fetches the page, only replace in its <body>, and merges its <head>, all without incurring the cost of a full page load.

for example:
Here is a post show page. The post's content save as raw html markup tags in db column,and render it in post show page
$('div.post-content').html($.parseHTML(decodeURI(gon.con))); as this time ,the gon.con is passed by post#show action.
every post has different content = include_gon.If it was included in head tag ,all previewed post content would be merged in head. When navigation visit to another post content , the same content preview visit will be get from head and display it as a preview.

<script>
    //<![CDATA[
    something post content
    //]]>
    </script>

include gon in head tag will merge the change.
so the the correct approach is that include gon in body
Every visist , whether application visit or restoration visit, replaces the body. the content you render in page always is correspond with the post.

上一篇下一篇

猜你喜欢

热点阅读