简体   繁体   English

使用jQuery变量添加rails image_tag

[英]Add rails image_tag with jQuery variable

I am adding a rails image_tag with jQueryand I am trying to put a jQueryvariable in the image_tag. 我正在使用jQuery添加rails image_tag,并且试图在image_tag中放置一个jQueryvariable。

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. 感谢您的所有帮助。

What about storing the content of your variable as pure Ruby and then use it in your image tag? 如何将变量的内容存储为纯Ruby,然后在图像标签中使用它呢?

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>

You're mixing two things there, the var variable = 0 is javascript and the <%= image_tag @users[variable].profile_picture is ruby. 您在这里混合了两件事, var variable = 0是javascript,而<%= image_tag @users[variable].profile_picture是ruby。 If you want them to be used together, the variable has to be parsed by js like this. 如果希望它们一起使用,则必须由js这样解析variable

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM