在jQuery中添加rails image_tag

时间:2017-05-28 22:02:15

标签: jquery ruby-on-rails

我想知道有没有办法用jQuery动态添加rails image_tag。

$(targetPlayerElement).html("<%= image_tag 'User.find_by_id(27).profile_picture'%>");

这是我到目前为止所尝试的。

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

您可以尝试:

$(targetPlayerElement).html('<%= image_tag User.find_by_id(27).profile_picture %>');

那必须看起来像:

$(targetPlayerElement).html('<img src="src" alt="46" />')

如果您在控制台中出现Uncaught SyntaxError: missing ) after argument list错误,那么image_tag位于双引号内,而src的{​​{1}}也会包含在双引号中引号。

如果image_tag字符串已经引用,则无需引用User.find_by_id(27).profile_picture的结果。

哪个版本的Rails?使用find代替find_by_id

另外,使用单引号括起image_tag打印的内容。