Reputation: 10207
In my Rails 4 app I have a TagHelper
with this function:
def robots_tag
if important_page?
tag(:meta, :name => "robots", :content => "dofollow")
end
end
In my main layout I am using it like this:
<head>
<%= robots_tag %>
</head>
How can I prevent Rails from showing an empty line in the source code if important_page?
is false
?
Thanks for any help.
Upvotes: 0
Views: 133
Reputation: 485
There's no way within the <%= %>
-Statement to 'remove' the empty line, because there's a newline character just before (in the HTML, after the <head>
-tag). If you really want to avoid an empty line, you need to remove this character by putting the ruby-tag/s right after the <head>
-tag (the following code assumes the test for important_page?
is removed from the method):
<head><% if important_page? %>
<%= robots_tag %><% end %>
</head>
(Side-note regarding the first answer: while the minus sign does remove the newline character, it doesn't remove the leading spacing, which results in just a different kind of ugly source code)
Upvotes: -1
Reputation: 32933
Try adding a minus sign at the end of the erb tag:
<%= robots_tag -%>
Just out of interest, why do you want to get rid of the newline? Even if it was in the body it wouldn't affect the result (visible to the user at least); in the head it seems even less important.
Upvotes: 2