【问题标题】:Outputting a Ruby array in HTML以 HTML 格式输出 Ruby 数组
【发布时间】:2011-02-08 22:56:24
【问题描述】:

我有一个在视图中行为不端的辅助方法:

module WeeksHelper

  # This is to create an array for a JS chart -
  # this creates an array to insert 
  # into a JS options hash such as [1,2,3,4,5] but when
  # this is outputted to the HTML, the array appears like this:
  # [12345]. How do I reinsert the commas in the view?
  def chart_xs(weeks)
    1.upto(weeks.count).to_a
  end

end

【问题讨论】:

    标签: javascript ruby-on-rails ruby arrays helpers


    【解决方案1】:
    1.upto(weeks.count).to_a.inspect
    (1..weeks.count).to_a.inspect # alternative
    #=> "[1, 2, 3, 4, 5]"
    

    或者

    # If you don't want the square brackets
    1.upto(weeks.count).to_a.join(',')
    #=> "1,2,3,4,5"
    

    【讨论】:

      【解决方案2】:

      一种可能性:

      def chart_xs(weeks)
        1.upto(weeks.count).to_a.inspect
      end
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-15
        • 2011-05-28
        • 2015-02-20
        • 2014-12-06
        • 1970-01-01
        相关资源
        最近更新 更多