RDFa On Rails

The Semantic Web made simple

what basics usage extend erb typo contact


Get Version 0.0.8

The rdfa library for Ruby On Rails adds a set of helper methods to the ApplicationHelper of your rails application providing developers with abstractions for the publishing of RDFa data. The library also contains a theme for typo which embed structural rdfa data in your blog.

RDFa On Rails supported vocabularies


sudo gem install rdfa

RDFa On Rails is developed and maintained by Cédric Mesnage

The basics

In your environment.rb :

require 'rdfa'
In your layout replace <html> by :
<%= rdfa_html %>

RDFa On Rails provides 3 types of methods which we call the class methods, the property methods and the link_to methods. All class methods have the same signature, as well as the property methods and link_to methods respectively, here they are:

rdfa_class_method(uri=nil, &block)
rdfa_property_method(object=nil, subject_uri=nil, &block)
rdfa_link_to_method(name, options = {}, html_options = {},

Let us consider the class method "rdfa_resource" (see the vocabularies page for a complete list of available methods), here is how to use :

<% rdfa_resource do %>
	put stuff which speak of the resource, 
	such as property methods
<% end %>

So what do we have? well we call the method and pass it a block, the content of the block speaks about the resource, but which resource, well in this case we didn't specify it so it is a blank node, otherwise we should give it a URL :

<% rdfa_resource "http://the_url_of_this_resource.truc" do %>
	put stuff which speak of the resource, 
	such as property methods
<% end %>

Now the content describes the resource with the URL we specified. So how do we speak of a resource, we use property methods, let's try with "rdfa_title" :

<% rdfa_resource "http://the_url_of_this_resource.truc" do %>
	<%= rdfa_title "Machine Readable Title" %>
<% end %>

We call the method and pass it the machine readable title which in this case will also be the human readable title, but what if they differ, for instance I want the title to be fully capitalize for humans :

<% rdfa_resource "http://the_url_of_this_resource.truc" do %>
	<% rdfa_title "Machine Readable Title" do %>
		<%= "Human Readable Title".upcase %>
	<% end %>
<% end %>

And that's it, well there is something more, occasionally you might want to describe a resource without using a class method, then you use a property method and pass it the URL of the resource you speak of as in :

<% rdfa_title "Machine Readable Title", "http://the_greatest_url_ever.machin" do %>
		<%= "Human Readable Title".upcase %>
<% end %>

Again you can pass it a block if the machine and human readable objects differ. The last thing we have to see are link_to methods :

<%= rdfa_link_to_foaf_knows "Cédric", "http://cedricmesnage.org#me" %>

RDFa Link_to methods behave exactly like rails link_to methods.

Demonstration of usage

You do not have to modify anything in your models or controllers. Here are some examples on how you can embed RDFa data in your views.

A resource :

<% rdfa_resource url_for(resource) do %>
    <h3><%= rdfa_label resource.name %></h3>
    <p><%= rdfa_description resource.text %></p>
    <h4>Created by : <%= rdfa_creator url_for(resource.author)%></h4>
<% end %>

A person :

<% rdfa_person url_for(person) do %>
    <h3><%= rdfa_name person.name %></h3>
    <h2><%= rdfa_foaf_address person.address %></h2>
    <h2><%= rdfa_mbox person.email %></h2>
    <% for contact in person.contacts %>
        <%= rdfa_knows url_for(contact) do %>
            <h4><%= contact.name %></h4>
        <% end %>
    <% end %>
<% end %>

A semantic blog post :

<% rdfa_post url_for(post) do %>
    <% rdfa_title post.title do %>
        <h1><%= link_to post.title, {:controller => :posts, :action => :show, 
        :id => post.id} %></h1>
    <% end %>
    <h4><%= rdfa_creator post.author %> the <%= rdfa_date post.created_at %></h4>
    <div><%= rdfa_post_content post.content %></div>
    <h5>Tags : 
        <% for tag in post.tags %>
            <%= rdfa_link_to_scot_tag tag.term, url_for(tag) %>
        <% end %>
<% end %>

The famous license:

<%= rdfa_license "http://your_prefered_license_url.org/" %>

Extending RDFa On Rails

Only some classes and predicates make it to the front page of the documentation of RDFa On Rails, you might want to use other vocabularies than the ones we selected. For this we provide you with an executable 'rdfa-vocabulary' which extracts from a given namespace the vocabulary definition. For instance for the RDFS vocabulary we typed:

$ rdfa-vocabulary rdfs 'http://www.w3.org/2000/01/rdf-schema#'
which outputs :
  # Vocabulary definition of rdfs
  RDFS = {
    :name => :rdfs,
    :fullname => "Resource Description Framework Schema",
    :description => "",
    :url => "http://www.w3.org/TR/rdf-schema/",
    :namespace => "http://www.w3.org/2000/01/rdf-schema#",
    :classes => {"Container"=>"The class of RDF containers.", "Literal"=>"The clas..."},
    :properties => {"isDefinedBy"=>"The defininition of the subject reso..."}

Then you copy paste this dictionary somewhere in your code and load it by doing :

Rdfa.register_vocabulary RDFS

Quite simple no? If this doesn't work because your vocabulary is weird or you want full control, you can still do it "by hand" :

Rdfa.register_rdfa_namespace 'sioc', 'http://rdfs.org/sioc/ns#'

Then register the classes you want to use of the vocabulary:

Rdfa.register_rdfa_classes 'sioc', ['Forum','Post']

This generates the methods rdfa_sioc_forum and rdfa_sioc_post which have the same behaviour as other class helpers. Now we can register some properties :

Rdfa.register_rdfa_properties 'sioc', ['content','creator_of']

et voilà! Now you can use the newly created classes and predicates anywhere in your views such as:

<% rdfa_sioc_post "http://example.org/post1" do %>
    <p><%= rdfa_sioc_content "a post content"%></p>
<% end %>

Now you wonder why we have some shortcuts properties and classes already available such as rdfa_post, rdfa_label or rdfa_description. The point is to give an early access to the most common terminology used in web development, then once you get used to RDFa and can go deeper in publishing your data, you can start using more specific vocabularies.

RDFa in erb templates

RDFa On Rails comes with a generator which takes as input erb/rails templates and outputs html, this way you can use RDFa On Rails without Rails. The command:

$ rdfa-generate index.rhtml
run in the same directory as the "index.rhtml" erb template generates the ouput in the "index.html" file. That's what we use to generate this page and the vocabulary page.

RDFa for typo

The RDFa on Rails lib includes a theme for typo which changes your blog into a semantic web blog producing RDFa data. To install the theme, go to the themes directory of your typo installation and run :

$ cd your_typo_installation_path/themes/
$ rdfa-typo

That’s it. Now go to the admin section of your blog and activate the rdfa-typo theme.

Contact, Mailing list, Forum, Questions

Please use the forum provided by rubyforge for any question you might have.

Mailing lists


Comments and suggestions are welcome. Send an email to Cédric Mesnage.



This code is free to use under the terms of the MIT license.