Rails - Inserting Variables inside an image_tag
I'm trying to create a dynamic image_tag url. 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:
"#{::Rails.root.to_s}/member.gif?id=#{@member.id}".html_safe
raw "#{::Rails.root.to_s}/member.gif?id=#{@member.id}"
Use double quotes, inside single quotes there is no variables processing.
精彩评论