How do I access URL parameters in a Mailer view?

I have the following working class Preview:

class UserMailerPreview < ActionMailer::Preview
  def invite
    USerMailer.invite
  end
end

I am trying to pass parameters to a method as follows:

localhost:3000/rails/mailers/user_mailer/invite?key1=some_value

The server seems to have received them:

Parameters: {"key1"=>"some_value", "path"=>"user_mailer/invite"}

But when I try to access them with a hash params, I get an error message.

Can I access these options in preview mode, and if so, how?

+4
source share
2 answers
+3

ActionMailer.

# 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
+1

All Articles