1

Im trying to grab the ids of the images using rails, pass those ids to javascript and then flip through them using a javascript Image Viewer.

This is what I have in my rails controller

@rails_array = Images.all


this is in my view file

<script type="text/javascript" >
 var myIds=new Array(<% @rails_array %>);
</script>

3 Answers 3

3

You can collect Ids from controller like this:

@rails_array = Images.all.map &:id

In the View:

<script type="text/javascript" >
   var myIds= <%= @rails_array %>
</script>
Sign up to request clarification or add additional context in comments.

Comments

2

A good way to pass any object to javascript is through JSON:

var myId= <%= @rails_array.to_json %> ;

Note: You'll need to append .html_safe for strings & hashes (or arrays containing them), but it isn't needed for arrays of integers.

Comments

1

Hi have you tried split() method of javascript

 <script type="text/javascript">
  var myId=<% @rails_array %>.split(/,/);
 </script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.