0

I'm adding a confirmation dialog to a delete operation in my rails tutorial, and I want to include some text from the object to be deleted. So I tried this:

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

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

I've changed it to use format strings and it's working fine:

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

The substitution happens: the resulting dialog says Really delete blog "Of cabbages and kings"?

What do I change to make the more-readable "#{article.title}" work for me? What's the difference?

Paul Hicks
  • 13,289
  • 5
  • 51
  • 78
  • The other question (vote to close as duplicate) is easier to find on google and has a very helpful answer. – Paul Hicks Apr 23 '15 at 04:55
  • Perhaps a duplicate, but I do believe that this discussion could be helpful to future ruby-ists. The question relates to the specific issue of double quotes vs. single quotes with the `#{}` interpolation (the other one only mentions the difference in passing). Perhaps the question / answers should be edited to make them more focused on this specific issue (subject could be: "why doesn't the `#{}` work" or similar). – Myst Apr 23 '15 at 05:32
  • Closing doesn't mean deleting. It just means no more answers are needed here. I think your answer covers everything needed on this question. – Paul Hicks Apr 23 '15 at 08:50

2 Answers2

4

The reason the #{} didn't work is the difference between double quotes " and single quotes '

EDIT: For a more complete description of string interpolation and using the #{} style, read this question. to see why double-quotes vs. single quotes don't normally pose a meaningful performance issue, read here. - credit to @PaulHicks for this edit.

Text in single quotes isn't pre-processed (parsed before creating the String object) while text in double quotes is pre-processed. So that:

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

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

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

EDIT: As @Stefan suggested, you could also use the %Q() notation to avoid escaping:

confirm: %Q(Really delete blog "#{article.title}"?)

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

Good luck!

Community
  • 1
  • 1
Myst
  • 18,516
  • 2
  • 45
  • 67
  • Aha. I've obviously been away from Ruby for too long, I forgot that. I've been looking for a canonical question that this one duplicates; so far I've found half-a-dozen 0-upvote and 1-upvote equivalents, but nothing that stands out.. seems like there should be a canonical question for this.. – Paul Hicks Apr 23 '15 at 04:51
  • You can also use the `%Q(...)` literal to avoid escaping: `confirm: %Q(Really delete blog "#{article.title}"?)` – Stefan Apr 23 '15 at 07:33
  • Thanks for clarifying that, @PaulHicks . I wasn't aware of the distinction. And thanks @ for providing a cleaner option. I'll edit the answer with the tip. – Myst Apr 23 '15 at 12:26
2

Try this way, look at " " and ' '

"Really delete blog '#{article.title}'?"
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
jon snow
  • 3,062
  • 1
  • 19
  • 31