ruby - Does #{}-style substitution work in ERB expressions? -


this question has answer here:

i'm adding confirmation dialog delete operation in rails tutorial, , want include text object deleted. tried this:

<%= link_to 'delete', article_path(article), method: :delete, data: { confirm: 'really delete blog "#{article.title}"?' } %> 

the substitution not happen: resulting dialog says really delete blog "#{article.title}"?.

i've changed use format strings , it's working fine:

<%= link_to 'delete', article_path(article), method: :delete, data: { confirm: 'really delete blog "%s"?' % article.title } %> 

the substitution happens: resulting dialog says really delete blog "of cabbages , kings"?

what change make more-readable "#{article.title}" work me? what's difference?

the reason #{} didn't work difference between double quotes " , single quotes '

edit: more complete description of string interpolation , using #{} style, read this question. see why double-quotes vs. single quotes don't pose meaningful performance issue, read here. - credit @paulhicks edit.

text in single quotes isn't pre-processed (parsed before creating string object) while text in double quotes pre-processed. that:

'hello\n "world"!' == "hello\\n \"world\"!" #=> true 

the following change have worked fine (notice double quotes instead of single quotes):

<%= link_to ... confirm: "really delete blog \"#{article.title}\"?" } %> 

edit: @stefan suggested, use %q() notation avoid escaping:

confirm: %q(really delete blog "#{article.title}"?) 

(you can see more options in link above, regarding interpolation)

good luck!


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -