简体   繁体   English

Rails - 在image_tag中插入变量

[英]Rails - Inserting Variables inside an image_tag

I'm trying to create a dynamic image_tag url. 我正在尝试创建一个动态的image_tag网址。 Here is what I have 这就是我所拥有的

<%= image_tag('#{::Rails.root.to_s}/member.gif?id=#{@member.id}&d=#{@dog,id}') %>

But it's not rendering with the actual vars, it's rendering exactly as above. 但它不是用实际变量渲染的,它的渲染完全如上所述。

Ideas? 想法?

The problem is you are using single quotes. 问题是你使用的是单引号。 Try this instead: 试试这个:

<%= image_tag("#{::Rails.root.to_s}/member.gif?id=#{@member.id}") %>

The #{} operator won't be executed inside of single quoted strings. #{}运算符不会在单引号字符串中执行。

To answer a question in the comments, to mark a string as html safe, meaning that HTML special characters should not be escaped, you can use the raw or html_safe functions: 要回答评论中的问题,要将字符串标记为html安全,意味着不应转义HTML特殊字符,您可以使用raw或html_safe函数:

"#{::Rails.root.to_s}/member.gif?id=#{@member.id}".html_safe
raw "#{::Rails.root.to_s}/member.gif?id=#{@member.id}"

使用双引号,单引号内没有变量处理。

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

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