views:

69

answers:

3

I can't figure out what I'm doing wrong here.

I can’t seem to get the #{@user.name} to work in my flash[:notice]

Everything else works just fine I can add new users, but when I add a new user instead of saying “User John Doe was successfully created”, it says “User #{@user.name} was successfully created.”

I'm at this point in the depot app: depot_p/app/controllers/users_controller.rb to work.

+2  A: 

Ok, I figured this out.

Flash notices must be in double quotes, not single quotes. I had:

flash[:notice] = 'User #{@user.name} was successfully created.'

When it should have been flash[:notice] = "User #{@user.name} was successfully created."

That's a tricky detail for a total noob. FYI for others out there.

Lee McAlilly
Upvoted because you answered your own question so quickly.
MJB
@Lee: actually that's not the reason. The #{} string interpolation mechanism works for double quotes only, not single quotes. The flash part is irrelevant here.
JRL
+7  A: 

This has to do with string interpolation which is for double quoted strings only, it has nothing to do with the flash.

You can substitute the value of any Ruby expression into a double quoted string using the sequence #{ expr }. If the expression is just a global variable, a class variable, or an instance variable, you can omit the braces.

"Seconds/day: #{24*60*60}"    » Seconds/day: 86400
"#{'Ho! '*3}Merry Christmas"  » Ho! Ho! Ho! Merry Christmas
"This is line #$."            » This is line 3

For more info, Programming Ruby is a free Ruby online book.

JRL
A: 

Agree with JRL. If you want to use the #{foo} syntax in any string you need to use double quotes.

Kevin