0

I am adding a rails image_tag with jQueryand I am trying to put a jQueryvariable in the image_tag.

var variable = 0;
$(targetPlayerElement).html('<%= image_tag @users[variable].profile_picture 

I am getting undefined local variable or method `variable' error. What is the correct syntax to do this if you even can. thank you for all the help.

2
  • What is @users? Commented May 31, 2017 at 15:51
  • users is from the controller users = User.limit(5).order("RANDOM()") Commented May 31, 2017 at 15:54

2 Answers 2

1

What about storing the content of your variable as pure Ruby and then use it in your image tag?

An example:

<div id="content"></div>
<% image = 'http://placekitten.com.s3.amazonaws.com/homepage-samples/408/287.jpg' %>
<script>$('#content').html('<%= image_tag image %>')</script>
Sign up to request clarification or add additional context in comments.

1 Comment

I agree that using compact first makes sense.
0

You're mixing two things there, the var variable = 0 is javascript and the <%= image_tag @users[variable].profile_pictureis ruby. If you want them to be used together, the variablehas to be parsed by js like this.

var variable = 0;
$(targetPlayerElement).html("<%= image_tag @users[" + variable + "].profile_picture %>")

3 Comments

I don't think this will work, since variable is set after <%= ... %> is computed.
When i try that I get no implicit conversion of String into Integer error.
@Gerry you're right @jrocc try and use only ruby ``` <% variable = 0 %> $(targetPlayerElement).html("<%= image_tag @users[" + variable + "].profile_picture %>") ```

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.