gpt4 book ai didi

ruby-on-rails - Rails 4.1 强制 ActionMailer 返回其他值

转载 作者:数据小太阳 更新时间:2023-10-29 08:03:33 25 4
gpt4 key购买 nike

我如何强制在 ActionMailer 方法中返回另一个值。示例:

class TestMailer < ActionMailer::Base

extend MandrillApi

def index(email, code)
@code = code
result = MandrillApi.send({subject: t('test.index.subject'), to: email, template: render_to_string})
return result
end
end

在这种情况下,我使用 ActionMailer 来呈现模板 (render_to_string) 并将变量传递给 View ,但我需要从 MandrillApi 模块获取结果值。当我调用方法时 TestMailer.index("xxxx@gmail.com", "asdf123")返回值是 #<ActionMailer::Base::NullMail:0x007fe5c433ab10>

谢谢!!

最佳答案

为该任务创建单独的类(不是 ActionMailer::Base 的后代)。 ActionMailer 将重写其邮件程序操作的返回值。

您可以使用 render_anywhere gem 作为解决方案:

require 'render_anywhere'

class MandrilMailer
extend MandrillApi
include RenderAnywhere

# We need that for 't' i18n helper to work
include ActionView::Helpers::TranslationHelper

class RenderingController < RenderAnywhere::RenderingController
# we can use that for url_helpers to work properly
def default_url_options
host = {'production' => 'production.example.org', 'development' => 'development.example.org'}[Rails.env]
{host: host}
end
# alternatively, you can add this line inside you config/environments/*.rb files, setting your own host:
# Rails.application.routes.default_url_options[:host] = 'example.org'
end
def index(email, code)
# 'render' is a RenderAnywhere call
MandrilApi.send({subject: t('test.index.subject'), to: email, template: render(template: 'test_mailer/index', locals: {code: code}, layout: false)})
end
end

MandrilMailer.new.index("user@example.org", "asdf123") # => result of MandrilApi.send

关于ruby-on-rails - Rails 4.1 强制 ActionMailer 返回其他值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31861153/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com