Rails array to javascript

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 :frowning:

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

Any suggestions are much appreciated,

Thanks,

  • Jon

On Apr 30, 6:48 am, Michael L. <rails-mailing-l…@andreas-
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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs