【问题标题】:How to display in view formated from hash text with jQuery?如何使用 jQuery 在从哈希文本格式化的视图中显示?
【发布时间】:2017-11-19 01:20:10
【问题描述】:

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 = @hash.values 这样发布@result 我可以看到结果为[["a", "b", "c"], ["x", "y", "z"]],但@result = @hash.values.map{ |v| v.join(',') }.join("\n") 不起作用?

.join("\n") 似乎有问题,但我需要保存选项才能从新行查看每个格式化值

有什么办法吗? 谢谢

【问题讨论】:

    标签: jquery ruby-on-rails hash


    【解决方案1】:

    你可以使用simple_format助手:

    <%= simple_format(@result) %>
    

    还有一个pretty neat code可以在jQuery中做

    您可能感兴趣的另一个答案:raw vs. html_safe vs. h to unescape html

    【讨论】:

      猜你喜欢
      • 2015-02-15
      • 2015-06-02
      • 2015-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-22
      相关资源
      最近更新 更多