加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 将变量传递给块 – Rails

发布时间:2020-12-17 03:29:30 所属栏目:百科 来源:网络整理
导读:参见英文答案 How does instance_eval work and why does DHH hate it?????????????????????????????????????3个 在我的rails应用程序中,我使用此gem与Gmail API进行交互: https://github.com/gmailgem/gmail 以下是我发送电子邮件的方法: gmail = Gmail.c
参见英文答案 > How does instance_eval work and why does DHH hate it?????????????????????????????????????3个
在我的rails应用程序中,我使用此gem与Gmail API进行交互: https://github.com/gmailgem/gmail

以下是我发送电子邮件的方法:

gmail = Gmail.connect(params[:email],params[:password])
@email = params[:email]

email = gmail.compose do
  to @email
  subject "Having fun in Puerto Rico!"
  body "Spent the day on the road..."
end
email.deliver!

我收到此错误:

An SMTP To address is required to send a message. Set the message smtp_envelope_to,to,cc,or bcc address.

电子邮件变量无法传递到块中.是什么造成的?如何传递动态电子邮件地址?

解决方法

我确定它发生了,因为@email是一个实例变量,绑定到self(有点等于self.email).并且gmail模块可以使用像instance_eval或class_eval这样的方法轻松更改内部块,即所谓的“范围门”.这是ruby元编程的常规功能.

只需使用一个简单的变量,它就会被延续捕获.

email_to = params[:email]
email = gmail.compose do
    to email_to
    ...
end

我强烈建议不要将实例变量用作temp – 它们代表对象的状态.使用局部变量,这就是它们的设计目标.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读