Passing parameters to MailView or ActionMailer :: Preview in Ruby on Rails
2 answers
It is still an actual question and there are still very few solutions that can be found on the Internet (especially elegant ones). I hacked my way through this one today and came up with this solution and blog post about the ActionMailer extension .
# config/initializers/mailer_injection.rb
# This allows `request` to be accessed from ActionMailer Previews
# And @request to be accessed from rendered view templates
# Easy to inject any other variables like current_user here as well
module MailerInjection
def inject(hash)
hash.keys.each do |key|
define_method key.to_sym do
eval " @#{key} = hash[key] "
end
end
end
end
class ActionMailer::Preview
extend MailerInjection
end
class ActionMailer::Base
extend MailerInjection
end
class ActionController::Base
before_filter :inject_request
def inject_request
ActionMailer::Preview.inject({ request: request })
ActionMailer::Base.inject({ request: request })
end
end
+2
source to share