使用Mandrill时的Ruby引号语法

使用Mandrill时的Ruby引号语法,ruby,string,smtp,mandrill,mailer,Ruby,String,Smtp,Mandrill,Mailer,我有一个RubyonRails应用程序,我正在编写邮件程序向Mandrill发送SMTP邮件头。 我认为将变量转换为字符串有问题: class MyMailer < Devise::Mailer helper :application # gives access to all helpers defined within `application_helper`. include Devise::Controllers::UrlHelpers # Optional. eg. `conf

我有一个RubyonRails应用程序,我正在编写邮件程序向Mandrill发送SMTP邮件头。 我认为将变量转换为字符串有问题:

class MyMailer < Devise::Mailer

helper :application # gives access to all helpers defined within `application_helper`.
include Devise::Controllers::UrlHelpers # Optional. eg. `confirmation_url`

def confirmation_instructions(record, token, opts={})

     headers['X-MC-MergeVars'] = '{"ctoken1": "Drip Email"}'

     temp_var = "Hello Everyone"
     headers['X-MC-MergeVars'] = '{"tag_test_1": "yoyoyo"}'           # 1

    # headers['X-MC-MergeVars'] = "{'tag_test_1': 'yoyoyo'}"          # 2
    # headers['X-MC-MergeVars'] = "{'tag_test_1': #{temp_var}}"       # 3
    #  headers['X-MC-MergeVars'] = '{"tag_test_1": #{temp_var}}'      # 4

     headers['X-MC-MergeVars'] = '{"ctoken3": "Test at the End"}'

     super

end

end
class MyMailer
或者这个:

headers['X-MC-MergeVars'] = %Q'{"tag_test_1": "#{temp_var}"}'

首先,需要注意的是,不能在单引号中使用{}语法。您只能在双引号中使用它。还要注意,当您使用新的哈希语法
{'':'}
时,它会将字符串转换为符号。上面的答案应该有用。

spickermann太棒了——而且反应速度快得可笑!非常感谢-这非常有效。我想我需要摆脱引号中的角色——我无法完全理解为什么我需要它,但我现在可以继续了。