The Pied Pipes
The Pied Pipes

Reputation: 1435

Outputting a Ruby array in HTML

I've got a helper method that is misbehaving in the view:

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

Upvotes: 0

Views: 159

Answers (2)

seeingidog
seeingidog

Reputation: 1206

One possibility:

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

Upvotes: 1

Phrogz
Phrogz

Reputation: 303540

1.upto(weeks.count).to_a.inspect
(1..weeks.count).to_a.inspect # alternative
#=> "[1, 2, 3, 4, 5]"

Or

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

Upvotes: 2

Related Questions