2013-08-20 48 views
1

我有一個郵件看起來像(@user@foo傳遞到含方法):我有一些放測試它串插不打印變量的值,在打印精確的字符串

mail(to: @user.email, subject: 'Foo is expired', 
    body: 'Your Foo reservation for #{@foo.bar.name} in position #{@foo.position} 
    has expired. Please recreate the reservation if necessary') 

,把mail.body看起來像:

'Your Foo reservation for #{@foo.bar.name} in position #{@foo.position} 
has expired. Please recreate the reservation if necessary' 

我只是誤認爲我如何做插值?它與ActionMailer有什麼關係,或者將文本輸出到控制檯?

謝謝。在ruby

+1

使用'%Q考慮一個字符串,不引用,像這樣的字符串插值:#{ruby_variable}!'如果你想使用引號但不能轉義它們(引用:http://en.wikibooks.org/wiki/Ruby_Programming/Alternate_quotes) – MrYoshiji

回答

6

路線插值只能用雙引號""

+1

或者heredocs,'%Q {...}','/.../'和'%r {...}'。 –

3

工作插值工作,則需要用雙引號"不是單引號'

> a = 'test' 
=> "test" 
> 'Testing #{a}'  # This won't interpolate 
=> "Testing \#{a}" 
> "Testing {a}"  # This interpolates    
=> "Testing test"