Ruby on Rails and Adding Structured Markup Language to Your Content...

Creating a solid SEO base is critical for getting your self out there and making money or success or bees or whatever you want to make. Structured Data embedded in your page is one giant step forward using a little script and a little JSON-LD, and a little markup in your Rails html.erb file. It gives crawlers the clues and direction to actually make sense of what is in your page, what kind of content it holds, who wrote it, etc. And allows google to make what they call “rich snippets” or small examples of the content that show up in the google searches.

Adding Structred Data to your page is as simple as adding a <script> tag and as complicated as english grammar to a foreign speaker. Fantastic.
Basically we need to add a Javascript object into our page, but this object follows a VERY specific set of rules and regulations and needs a lot of hand holding. The good news is we should only need to do it a couple of times to cover your whole needs!

What We Need
A website, hopefully your own
Dynamic content (a Model), like say, a blog
The rules and regulations that govern Structured Data for a blog post
Testing, blah

How We Do
So to start I’m going to assume that you have a website, with some kind of model where you make new ones regularly. Like a blog, for all you millennials. That’s the easy bit. If your aren’t there yet you need to back up a few steps…
Next we need to get the right STRUCTURE for our blog post (or whatever kind of thing you are working with ie. recipe, comment, contact page, etc.). These are provided by I’m using a blogPosting structure, but you can find your own out of the many, many options HERE or HERE.  Now I used that first site and it gave me kind of the right information, and then I used the second site and it gave me the rest of the information and with our POWERS COMBINED I got a pretty okay chunk of code.
***WARNING ! I hate myself for this, but I have to warn you. I did the usual copy pasting bull shit from sites and hit a funny issue where it wouldn’t pass the tests. Just some bits. And those bits changed. WATCH THE QUOTATIONS! There is a difference between ” and " that was not picked up by IntelliJ Rubymine and does not get warned about in the Google Testing page. ***
Mine eventually looked like this;
<script type='application/ld+json'>
 "@context": "",
 "@type": "BlogPosting",
 "mainEntityOfPage": {
 "@type": "WebPage",
 "@id": "<%= @post.slug %>"
 "datePublished": "<%= @post.created_at %>",
 "dateCreated": "<%= @post.created_at %>",
 "dateModified": "<%= @post.updated_at %>",
 "headline": "<%= @post.title %>",
 "author": {
 "@type": "Person",
 "name": "Shane Glass"
 "articleBody": "<%= strip_tags(@post.try(:content)).gsub(/[^0-9a-z ]/i, ' ') %>",
 "copyrightHolder": "",
 "creator": "",
 "description": "<%= strip_tags(@post.try(:short_content)).gsub(/[^0-9a-z ]/i, ' ') %>",
 "image": "<%= @post.image_url(:large) %>",
 "publisher": {
 "@type": "Organization",
 "name": "Shattered Design",
 "logo": {
 "@type": "ImageObject",
 "url": ""
And just got tossed right at the end of the page. If we go through here line by line we can see how it works;
  • The first two lines are the type declaration ie. This is a blogPosting object found on the schema website.
  • This content is found on a website located at ….
  • It was published, created and modified at these dates.
  • Etc. Etc. You get it. Just pull all the pertinent information from your @post (or whatever) object and plop it into the object.
  • The hard bit came when I got to the description and articleContent lines…
The Hard Bit
Wasn’t actually that hard… just finicky. Turns out JSON-LD doesn’t like SOME punctuation. Not all, just some. Also HTML. Specifically in any field that holds lots of text like the description or the content… You know, the ones filled with punctuation and HTML. Now sentence structure and punctuation isn’t SUPER important when it comes to SML because we are just giving the bots a guide, so when I put in the data for my content and description, I just sanitised it and called it good. Your mileage may vary but since my content was filled with code snippets and styling this seemed like a good everyday option;
"articleBody": "<%= strip_tags(@post.try(:content)).gsub(/[^0-9a-z ]/i, ' ') %>",
This gets the Post Content, strips the tags thanks to the helper, and replaces all punctuation with a space. Simple! 
You should now have a dynamic template for creating Structured Markup Language on your site!

Comments (Coming Soon)