Single quote string string interpolation

后端 未结 3 926
借酒劲吻你
借酒劲吻你 2021-01-12 20:18

I am trying to set an email address within ActionMailer with Rails. Before it was hard coded, but we want to make them ENV variables now so we don\'t need to amend code each

3条回答
  •  无人共我
    2021-01-12 20:31

    You cannot use string interpolation with single-quoted strings in Ruby.

    But double-quoted strings can!

    from = "'Name of Person' <#{ENV['EMAIL']}>"
    

    But if you want to keep your double-quotes wrapping the Name of Person, you can escape them with a backslash \:

    from = "\"Name of Person\" <#{ENV['EMAIL']}>"
    

    Or use string concatenation:

    from = '"Name of Person" <' + ENV['EMAIL'] + '>'
    # but I find it ugly
    

提交回复
热议问题