Rails 自动将我的代码插入 body 标签而不是 head 标签

Rails insert automatically my code into body tag not into head tag

我在 application.html.erb

<html>
  <head>

     <%if @hasAdsense == true%>
     <script data-ad-client="ca-pub-64xxxxxxxxxxx" async 
      src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
     <%end%>
  
  </head>

  <body>
     <%= yield %>
  </body>
</html>

但是当我看到带有 chrome 开发人员工具的页面时,它会自动插入正文而不是头部。

所以 html 页面代码 @hasAdsense==true 看起来像这样。

<html>
  <head>
  </head>

  <body>
      <script data-ad-client="ca-pub-64xxxxxxxxxxx" 
      async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
  </body>
</html>

我不知道为什么会这样,但是 google bot 检查 head 标签中的 Adsense 脚本, 所以我认为这可能会导致我无法在我的网站中使用 Adsense 脚本。

有没有办法让 rails 不自动将 adsense 脚本插入 body 标签?

你有吗

<%if @hasAdsense == true%>
     <script data-ad-client="ca-pub-64xxxxxxxxxxx" async 
      src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<%end%>

在任何其他视图中,因为它可能来自 yield

我们这里可能需要更多信息,@hasAdsense 是布尔值吗(您可以通过显示控制器或直接查看页面来确认)吗?其他内容会发生这种情况吗?所以如果你用 <title>Hello World</title> 做了一个 if 语句,它也会在正文中呈现吗?