Problem displaying usernames in my flash [: notification] - Agile Web Development With Rails - Chapter 11

I cannot figure out what I am doing wrong here.

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

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

I am currently in the depot application: depot_p / app / controllerlers / users_controller.rb to work.

+2


a source to share


3 answers


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

You can substitute the value of any Ruby expression in a double-quoted string using a sequence #{ expr }

. If the expression is just a global, class variable, or instance variable, you can omit the curly 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 information, Programming Ruby is a free online Ruby book.

+8


a source


Ok I figured it out.

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

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



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

This is a tricky detail for a complete noob. FYI for others.

+2


a source


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

0


a source







All Articles