如何在特定环境的电子邮件中发送 URL?
使用restful_authentication生成的代码,我们通常会在注册和激活邮件中获取“localhost”作为URL。 我正在尝试创建这样的东西:
def signup_notification(user, host)
setup_email(user)
@subject += 'Please activate your new account'
@body[:url] = "http://#{host}/activate/#{user.activation_code}"
end
但是,由于传递邮件的实际调用是在观察者中,所以我不能这样做:
UserMailer.deliver_signup_notification(user, request.host_with_port)
由于请求方法在模型中不可用。 这样做的最佳方法是什么?
我考虑过将每个域的 URL 存储在 YAML 文件中,然后在启动时加载它,但端口可能会发生变化,因此它不起作用。
或者,我考虑在加载应用程序时设置的某个地方创建一个静态类变量,但我不知道如何执行此操作。 请求方法可用于初始化程序吗?
Using the code generated by restful_authentication, we usually get 'localhost' as the URL in the sign up and activation mails. I am trying to create something like this instead:
def signup_notification(user, host)
setup_email(user)
@subject += 'Please activate your new account'
@body[:url] = "http://#{host}/activate/#{user.activation_code}"
end
However, since the actual call to deliver the mail is in an Observer, I can't do this:
UserMailer.deliver_signup_notification(user, request.host_with_port)
Since the request method is not available in models. What is the best way to go about doing this?
I thought about storing URLs per domain in a YAML file and then loading it on startup but then the ports may change, so it wouldn't work.
Alternatively, I thought about creating a static class variable somewhere that is set upon loading up the application, but I don't know how to go about doing this. Is the request method available for initializers?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试 current_request 插件,参见 http://codeshooter.wordpress.com/2009/08/
为我工作
try current_request plugin, described at http://codeshooter.wordpress.com/2009/08/
worked for me
config/envrionments/development.rb
中设置它使用 ActiveMailer,我在模型
app/models/event_mailer.rb
中的,然后在邮件程序视图
app/ views/event_mailer/new_event.rb
在邮件中生成:
Using ActiveMailer, i set this in the
config/envrionments/production.rb
In the model
app/models/event_mailer.rb
And then in the mailer view
app/views/event_mailer/new_event.rb
Which Generates in the mail:
我也遇到了同样的问题,这就是我所做的
在用户模型中我将 :host 添加到 attr_accessible,然后添加此方法
,在发送电子邮件(如创建和激活)的方法中,我添加了
因为我仍然是一个菜鸟,所以我真的希望有人能出现针对此问题提供最佳实践解决方案。
I got a same problem too and here's what I've done
in user model I add :host to attr_accessible, then I add this method
in user controller, in the methods that sends email like create and activate I put
since I'm still a noob-on-rails so I really hope someone will come up with a best-practice solution for this problem.
您的服务器访问的主机名可以在请求的 Host 标头中找到。 您不应该在模型中访问它(假设 Rails 正常)。 但是,如果您可以访问控制器中的请求对象,则可以在从数据库中提取模型对象时将该值插入到模型字段中。
The hostname your server is accessed by is found in the Host header of the request. You shouldn't have access to it in the models (assuming rails is sane). But if you can access request objects in the controllers you can insert that value to your model field when you pull the model objects from db.