Ruby on Rails and Javascript

2

I have the following in the controller in Ruby on Rails:

  def index
    @categorias = Categoria.find(:all)
  end

In my View I have the following code to get the categories:

<script type="text/javascript">
    var teste = <%= @categorias %>;
    alert(teste.length);
</script>

In this case, nothing is returned. Can anyone help me?

    
asked by anonymous 17.06.2015 / 20:30

1 answer

2

Try this way:

<script type="text/javascript">
    var teste = <%= raw @categorias.to_json %>;
    alert(teste.length);
</script>
    
17.06.2015 / 20:56