为什么我的Rails应用程序中出现“invalid multibyte char”?

时间:2022-08-22 22:34:46

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

我正在尝试使用SMTP设置邮件程序并从控制台获取以下错误:

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:

user_mailer.rb:

class UserMailer < ActionMailer::Base
  default :from => "remindeal@gmail.com"

  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:

User.rb:

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

1 个解决方案

#1


1  

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

Ruby只接受单引号,双引号和反引号来引用字符串。他们每个人都有不同的含义。

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.

由于您使用多字节前向引用和反引用引用标头哈希的键,因此ruby会发出错误。我想你在一些网站上复制粘贴了一些美化源代码。

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.

请注意,通常,建议使用符号(:like_this)作为散列键,但对于上述情况,您需要使用单引号或双引号,因为它包含 - 作为键的一部分。

#1


1  

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

Ruby只接受单引号,双引号和反引号来引用字符串。他们每个人都有不同的含义。

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.

由于您使用多字节前向引用和反引用引用标头哈希的键,因此ruby会发出错误。我想你在一些网站上复制粘贴了一些美化源代码。

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.

请注意,通常,建议使用符号(:like_this)作为散列键,但对于上述情况,您需要使用单引号或双引号,因为它包含 - 作为键的一部分。