What is the Role of HTML5 in Rails 3

We’ve all heard that Rails 3 uses HTML5. That’s true, but the actual role that HTML5 plays in Rails 3 applications may be simpler than you suspect.

Before that you need to know few things

What rails.js does
  • It finds remote links, forms, and inputs, and overrides their click events to submit to the server via AJAX.

  • It triggers six javascript events to which you can bind callbacks to work with and manipulate the AJAX response.

Remember the first thing rails.js does? Before it can override the click and submit events of all the remote elements, it first needs to find all the remote elements.


Rails 3 takes advantage of this new valid markup, by turning all links and forms with :remote => true into HTML elements that have the tag data-remote=true.

<%= link_to "home", {:action => "index"}, :remote => true, :class => "button-link" %>
# => <a href="index" class="button-link" data-remote=true>home</a>

And now the rails.js finds all remote links with the selectors:


$('form[data-remote]')
$('a[data-remote],input[data-remote]')
 

… and overrides the submit and click actions, respectively, with an ajax request to the forms’ action links’ href properties. Then it does the same thing wirh remote forms and inputs, using the form’s action.

So, that’s it, HTML5 just provides a convenient, semantic, way for us to designate and select which elements to override.

 

Comments

Popular posts from this blog

Interview Question on Ruby on Rails

Inserting and Moving elements inside Ruby Array