23tux
23tux

Reputation: 14736

Print \n as actual newline in terminal

In my RSpec email tests I render some text views with newlines:

class MyMailer < ActionMailer::Base
  def email1
    @foo = "Hello"
    @bar = "World"
    mail(to: "[email protected]", subject: "Foobar")
  end
end

This is the view email1.text.erb

<%= @foo %>

<%= @bar %>

And this is my test:

it 'renders output' do
  body = MyMailer.email1.body
  puts body
end

When I print the email body in terminal, ruby returns \n instead of an actual newline:

Hello\n\nWorld

For debugging purpose it would be nice to have the actual newlines instead of \n printed in my terminal. Is there a way to do this?

Upvotes: 1

Views: 1743

Answers (3)

BroiSatse
BroiSatse

Reputation: 44685

It seems there is something off with your encoding. Since this is only for debug purpose, you can fix it with:

puts body.to_s.gsub('\n', "\n")

(Note the difference between quotes!). To fix it the correct way, you need to tell what is your body encoding as it is being used for transforming your raw message body to ruby string.

Upvotes: 3

I will suggest to do this in email1.text.erb

<%= @foo + '\n' + @bar %>

Upvotes: 0

boomcode
boomcode

Reputation: 399

you must escape characters. use \\n

Upvotes: -1

Related Questions