nelson
nelson

Reputation: 45

Why do I get "invalid multibyte char" in my Rails app?

I am trying to setup a mailer with SMTP and getting the following error (from console):

SyntaxError (/Users/nelsonkeating/Desktop/prelaunch/app/mailers/user_mailer.rb:6: invalid multibyte char (US-ASCII)
/Users/nelsonkeating/Desktop/prelaunch/app/mailers/user_mailer.rb:6: syntax error, unexpected $end, expecting ']'
    headers[‘X-MC-Track’] = "opens, clicks"
             ^):
  app/models/user.rb:37:in `send_welcome_email'

user_mailer.rb:

class UserMailer < ActionMailer::Base
  default :from => "[email protected]"

  def welcome_email(user)
    mail(:to => user.email, :subject => "Invitation Request Received")
    headers[‘X-MC-Track’] = "opens, clicks"
    headers[‘X-MC-GoogleAnalytics’] = "example.com"
    headers[‘X-MC-Tags’] = "welcome"
  end
end

User.rb:

35 def send_welcome_email
36    unless self.email.include?('@example.com')
37      UserMailer.welcome_email(self).deliver
    end
  end

Upvotes: 1

Views: 501

Answers (1)

shigeya
shigeya

Reputation: 4922

Ruby only accepts single quote, double quote and backquote to quote strings. Each of them has different meaning.

Since you're quoting keys of headers hash with multibyte forward quote and backquote, ruby emit the error. I guess you copy pasted some of prettified source codes on some web site.

You can replace above source like this, using single quotes:

headers['X-MC-Track'] = "opens, clicks"
headers['X-MC-GoogleAnalytics'] = "example.com"
headers['X-MC-Tags'] = "welcome"

Note that, typically, using symbols (:like_this) for a hash key is recommended, but for the case like above, you need to use either single quote or double quotes, since it contains - as the part of the keys.

Upvotes: 1

Related Questions