valid way to handle simple AJAX operations within a Rails app, and there
in doing so. There is one common GOTCHA however that trips up even the most experienced
Rails developer: forgetting to use
content to the DOM.
Does this sound familiar?
Have you ever written a
create.js.erb SJR template like the one above
and found that when you tested it in your app nothing happened? If so,
then you have experienced this pain.
Even after a couple run-ins with this issue, perhaps you still aren’t
exactly sure which situations require
And what the heck is the difference between
Are these interchangeable?
From the Rails documentation,
All we are doing is taking a string of text and making sure that it doesn’t contain any invalid characters when the browser tries to parse it. Simple!
For a more in-depth discussion, read here.
To escape or not to escape
Now that you know what
when to use it.
Here’s a short answer: always :)
More specifically, any time you are trying to render HTML as a string and insert it into the DOM you’ll want to escape it first.
Some common examples include
Not every situation requires
however you’ll find yourself getting into trouble more often by forgetting to escape as opposed to escaping unnecessarily.
What’s the deal with
You may have seen
j being used in some code samples.
j method is nothing more than an alias for
Now you know
SJR templates are a great tool to have in your Rails arsenal, and by
learning how and when to use
j) within those
templates, you will save yourself hours of headaches debugging an