I am trying to show attributes of a resource with javascript. I'm sure I'm making a simple mistake or omission.
I am trying to understand how
j render
works and why it is necessary. I know it has something to do with the scenario where a malicious user inputs javascript and it would be executed in the application which could cause bad things to happen.
Setup: Simple blog template in a rails app: rails g scaffold Blog title:string content:text
views/blogs/show.html.erb
<div id="blogContent"> Javascript should enter result of @blogger.content here: </div>
assets/javascripts/blogs.coffee
$document.on "page:change", ->
$blogPost = "<%= j render(@blog.content) %>"
alert($blogPost)
$('#blogContent').append($blogPost)
Here are the issues I'm running into:
The alert box is literally returning:
<%= j render(@blog.content %>
. I want it to return that blog's content.Once I get it functioning to where the blog's content is showing, I want to prove to myself that it is necessary to escape javascript so that malicious user input is not executed in my application. So for example, if inside the content a malicious user inputted:
<a href='DangerousSite.xyz'>Hacker says click for free stuff</a>
Then what would result from my app rendering@blog.content
would be a link that says:Hacker says click for free stuff
. Once I prove to myself that escaping javascript is necessary to prevent stuff like this, I want to be able to escape it and have the result literally show<a href="Dangerous Site">Hacker says click for free stuff</a>
, or whatever else way the rendered sanitized javascript looks like.