decoding xml characters in rails view

I am trying to render coffee script in a textarea. The coffee script is fetched from a file which has been saved with utf-8 encoding. I have tried using the gem htmlentities on the coffee script to no avail. The gem seems to work on other strings in that view, its just this output thats the problem.

The file content:

jason = ->
   alert "jason"
   return

The output of that file:

jason = ->\n  alert \"jason\"\n  return\n

My controller method

def create
item_to_delete = JsCoffee.last
item_to_delete.destroy if item_to_delete
@js_coffee = JsCoffee.new(js_coffee_params)
@result = @js_coffee.save
@code = @js_coffee.code
@success = 'Conversion complete'
coder = HTMLEntities.new
@output = coder.decode(@js_coffee.output)
respond_to do |f| 
  f .html{ redirect_to js_coffee_path(@js_coffee)}
    f.js 
      end
end

The model in which I save the file

before_create :empty_output

def empty_output
  if File.zero?('public/temp/js/output/output_temp_file.coffee')
    errors.add(:code, 'There was an error with the conversion, please check your code')
  else
    self.output = File.read('public/temp/js/output/output_temp_file.coffee', encoding: 'UTF-8')
    true
  end   
end

And the js.erb view file where I show the output of the file

<% if @result %>

  $('.alert').remove();
  editor2 = ace.edit('editor2')
  editor2.setTheme('ace/theme/textmate');
  editor2.getSession().setTabSize(2)
  editor2.getSession().setUseSoftTabs(true)
  editor2.setShowPrintMargin(false)
  editor2.setHighlightActiveLine(false)
  editor2.session.setFoldStyle('manual')
  editor2.getSession().setMode('ace/mode/coffee')
  editor2.setValue("<%= j @output %>");

  $('#new_js_coffee').prepend("<div class='alert alert-success'><%= j @success %>\
  <button type='button' class='close' data-dismiss='alert'>&times;</button>\
  </div>");


<% end %>

Answers


The answer was not in htmlentities but to print out the raw html as in:

editor2.setValue("<%= j raw @output %>");

Need Your Help

How to debug Crashed Dump File?

c# asp.net iis iis-7

I run Debug Diagnostic tool on my windows server 2008. It created dump files.

SBJson iOS parser methods are Deprecated?

ios json sbjson

I'm starting a new project with SBJson parser, which people seem to recommend as the best on the internet for new iOS projects. I'm having a really strage issue which is that the current methods S...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.