Ruby on Rails and Javascript

Asked

Viewed 85 times

2

I have the following in the controller on 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?

1 answer

2


Try this way:

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

    It shines a lot! Thank you very beast! Pity that has no way to give medal here...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.