Rails insert automatically my code into body tag not into head tag - ruby-on-rails

I did it in 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>
but when I see a page with chrome developer tools ,it's automtically inserted in body not in head.
so html code of page where #hasAdsense==true looks like this.
<html>
<head>
</head>
<body>
<script data-ad-client="ca-pub-64xxxxxxxxxxx"
async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
</body>
</html>
I don't know why this happens, but google bot inspect Adsense script in head tag,
so I think this can cause problem so that I can't use Adsense script in my website.
is there a way let rails not insert automatically adsense script into body tag?

do you have
<%if #hasAdsense == true%>
<script data-ad-client="ca-pub-64xxxxxxxxxxx" async
src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<%end%>
in any other views, because it might come from the yield.
We probs need a little more info here, is #hasAdsense a boolean value (can you confirm by showing your controller or checking on the page directly)? does this happen with other content? so if you made a if statement with <title>Hello World</title> does that render in the body as well?

Related

How to propely implement Google Adsense with Rails

In my application.html.erb,
<html>
<head>
<script data-ad-client="ca-pub-64xxxxxxxxxxx" async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
</head>
<body>
<%= yield %>
</body>
</html>
but with this way, all pages would contain Adsense scripts.
I wanna exclude Adsense in some pages.
So I tried it
<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>
I don't know this is proper way to do it.
How should I implement Google Adsense with Rails?

Why is my view file moving all my head content into the body section?

I'm very confused by how the application.html.erb file and the view files combine. I have in my application.html.erb file the following (minimized the content for 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 my view file:
<html>
<head>
<!-- View Specific Stylesheet -->
<link href="/css/ViewSpecific.css" rel="stylesheet" />
</head>
<body>
<!-- View Specific Body -->
</body>
What I would expect is that when these 2 files combine, the View specific stylesheet load in the <head> section, and the View Specific Body load in the <body> section. The final result actually sends the view specific stylesheet to the <body> section, after the navbar from application.html.erb is already loaded. This obviously produces the unwanted result that part of my body is loading before the stylsheet is loaded, and the first second the user views the page everything looks terrible.
You should make use of content_for if you want to render the view-specific head into the head section of application.html.erb. Use
<% content_for :head do %>
<!-- View specific head section -->
<link href="/css/ViewSpecific.css" rel="stylesheet" />
<% end %>
Since you yield :head in the application.html.erb, thats all to be done. And you don't need html tags in the views.
For more, see http://apidock.com/rails/v4.2.1/ActionView/Helpers/CaptureHelper/content_for

Angular.Dart - Using ng-cloak in Dartium

Getting my feet wet with Angular.Dart. Reviewing these Exercises on GitHub
In chapter 2, when running index.html I noticed the classic flicker before mustaches replaced with data.
Applied the usual 'data-ng-cloak="" ' only to be shown a blank screen.
Removed 'data-' and all is well.
Is this a Dartanium issue or is data-?? no longer applicable for Angular.Dart?
<!DOCTYPE html>
<html ng-app>
<head>
<title>Chapter Two - A Simple Recipe Book</title>
<link rel="stylesheet" href="../web/style.css">
</head>
<body>
<div recipe-book ng-cloak=""> <!-- blank screen if using 'data-ng-cloak' -->
<h3>Recipe List - {{ctrl.CtrlName}}</h3>
<ul>
<li class="pointer"
ng-repeat="recipe in ctrl.recipes"
ng-click="ctrl.selectRecipe(recipe)">
{{recipe.name}}
</li>
</ul>
<h3>Recipe Details - {{ctrl.CtrlName}}</h3>
<div><strong>Name: </strong>{{ctrl.selectedRecipe.name}}</div>
<div><strong>Category: </strong>{{ctrl.selectedRecipe.category}}</div>
<div><strong>Rating: </strong>{{ctrl.selectedRecipe.rating}}</div>
<div>
<ul>
<li ng-repeat="ingredient in ctrl.selectedRecipe.ingredients">
{{ingredient}}
</li>
</ul>
</div>
<div><strong>Directions: </strong>{{ctrl.selectedRecipe.directions}}</div>
</div>
<script src="packages/shadow_dom/shadow_dom.min.js"></script>
<script type="application/dart" src="../web/main.dart"></script>
<script type="text/javascript" src="packages/browser/dart.js"></script>
</body>
</html>
As far as I know the 'data-` prefix is not yet supported but work in progress.
here you find the discussion https://github.com/angular/angular.dart/issues/519
You need to add the CSS that supports ng-cloak
see also angular.dart seems to be slow

About some tags in rails program

i have got this program in ruby editor.My output comes out to be
<html>
<head><title> Ruby on Rails tutorial Sample App | <%= #title %></title>
<%= csrf_meta_tag %>
</head>
<body>
<%= yield %>
</body>
</html>
what is the error here and what is yield and csrf_meta_tag?
You don't seem to be using a server to render the views, it seems like you are rather loading the html directly on your browser.
Maybe the following link will help you get started:
http://guides.rubyonrails.org/getting_started.html
you need to rename your file from:
application.html to be application.html.erb so that it is going to interpret your embedded ruby commands.
Ref yield & content_for
Within the context of a layout, yield identifies a section where content from the view should be inserted. The simplest way to use this is to have a single yield, into which the entire contents of the view currently being rendered is inserted:
<html>
<head>
</head>
<body>
<%= yield %>
</body>
</html>
You can also create a layout with multiple yielding regions:
<html>
<head>
<%= yield :head %>
</head>
<body>
<%= yield %>
</body>
</html>
The main body of the view will always render into the unnamed yield. To render content into a named yield, you use the content_for method.

Grail partial view for ajax request

<g:if test="${!request?.xhr}">
<!doctype html>
<html>
<head>
<meta name="layout" content="home">
</head>
<body>
<div class="row-fluid">
</g:if>
AJAX
<g:if test="${!request?.xhr}">
</div>
</body>
</html>
</g:if>
I get error: Grails tag [sitemesh:captureBody] was not closed.
In Config.groovy I set grails.views.gsp.sitemesh.preprocess = false but this doesn't help.
What way to use partial view with if statement.
A better way to handle this in grails is to wrap a template containing the main content. For example:
//_body.gsp
AJAX
//view.gsp
<!doctype html>
<html>
<head>
<meta name="layout" content="home">
</head>
<body>
<div class="row-fluid">
<g:render template="body">
</div>
</body>
</html>
Then your controller can render the whole view on a regular request, or just the body on AJAX.
You can check request.xhr in the controller, and determine to render a template or a string based on the results of that if statement.

Resources