# Rails array to javascript

**URL:** <https://www.ruby-forum.com/t/rails-array-to-javascript/136264>\
**Category:** Rails\
**Created:** [April 30, 2008, 7:48am UTC](https://www.ruby-forum.com/t/rails-array-to-javascript/136264 "2008-04-30T07:48:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dreamwave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dreamwave](https://www.ruby-forum.com/u/dreamwave)\
**Post date:** [April 30, 2008, 7:48am UTC](https://www.ruby-forum.com/t/rails-array-to-javascript/136264/1 "2008-04-30T07:48:05Z")

</div>

The effect I’m trying to achieve here is basically as follows:

## test\_controller.rb

class TestController \< ApplicationController

def test  
end

def fetch  
@array = [1,2,3,4,5,6,7,8,9]  
end

end

## fetch.rhtml

TEST

\<%= x=0 %\>

And this would generate the output of…

Test - 1  
Test - 2  
Test - 3  
ect.

However it fails miserably ☹

Instead it just prints Test - 1 10 times, so its basically interpreting  
the x+=1 wrong.

Any suggestions are much appreciated,

Thanks,

- Jon

---

<div class="post-metadata">

**Author:** ![dreamwave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7feea3/32.png) [@dreamwave](https://www.ruby-forum.com/u/dreamwave)\
**Post date:** [April 30, 2008, 10:22am UTC](https://www.ruby-forum.com/t/rails-array-to-javascript/136264/2 "2008-04-30T10:22:59Z")

</div>

On Apr 30, 6:48 am, Michael L. \<rails-mailing-l…@andreas-  
[s.net](http://s.net)\> wrote:

> 

There is no ruby iteration here (ie this chunk of view executes from  
top to bottom with no looping, ie it’s not the x+=1 going wrong. just  
that from ruby’s point of view there is no loop. You either need to  
make that array exist in javascript land and then just iterate through  
it in javascript or do the iteration in your view itself (ie  
something like  
\<% @array.each do |value| %\>  
document.write(“Test - \<%=value%\>”)  
\<% end %\>

Fred
