2011-02-08 62 views
0

一個Ruby數組我有一個輔助方法是在視圖中行爲不端:1.4.3在HTML

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 

回答

2
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" 
+0

先來先接受..謝謝 – 2011-02-08 23:11:52

1

一種可能性:

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