开发者

Rails - Inserting Variables inside an image_tag

开发者 https://www.devze.com 2023-02-13 13:56 出处:网络
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}\') %>

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消