0

root.rb

root 'builders#result'
    post 'builders/calc'

builders_controller.rb

class BuildersController < ApplicationController

    def result
    end

    def calc
      @hash = {0 => ["a", "b", "c"],1 => ["x", "y", "z"]}
      @text = @hash.values.map{ |v| v.join(',') }.join("\n")
      @result = @text
    end
end

result.html.erb

<div id="total"></div>

calc.js.erb $('#total').html('<%= @result %>')

If I post @result like @result = @hash.values I can see the result as [["a", "b", "c"], ["x", "y", "z"]] but @result = @hash.values.map{ |v| v.join(',') }.join("\n") not working?

Seems like problem with .join("\n") but I need to save option to see each formated value from new line

Any fix? Thanks

anndrew78
  • 196
  • 1
  • 20

1 Answers1

0

You can use simple_format helper:

<%= simple_format(@result) %>

There is also a pretty neat code to do it in jQuery

Another answer that might interest you: raw vs. html_safe vs. h to unescape html

radubogdan
  • 2,744
  • 1
  • 19
  • 27