问题
how to i can force to return another value in ActionMailer method. Example:
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
In this case i use ActionMailer to render template(render_to_string) and pass variables to the view, but i need get result value from MandrillApi module. When i call the method TestMailer.index("xxxx@gmail.com", "asdf123")
the value return is #<ActionMailer::Base::NullMail:0x007fe5c433ab10>
Thanks!!
回答1:
Create separate class (not a descendant of ActionMailer::Base
) for that task. ActionMailer will rewrite return values from its mailer actions.
You can use render_anywhere gem as a solution:
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
来源:https://stackoverflow.com/questions/31861153/rails-4-1-force-actionmailer-to-return-other-value