我想知道有没有办法用jQuery动态添加rails image_tag。
$(targetPlayerElement).html("<%= image_tag 'User.find_by_id(27).profile_picture'%>");
这是我到目前为止所尝试的。
感谢您的帮助。
答案 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
打印的内容。