Search code examples
ruby-on-railsruby-on-rails-4actionmailerparametersruby-on-rails-4.1

How would I access url parameters in Mailer Preview


I have the following working Preview class:

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

I'm trying to pass paramaters to the method like so:

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

The server seems to receive them:

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

But when trying to access them with the hash params, I get an error.

Can I access these parameters in a Preview method and if so - how?


Solution

  • I dug into the code behind the mailer preview system and discovered that, unfortunately, none of the request parameters are passed to the preview class, and are thus inaccessible to the preview.

    The relevant controller action is in railties: Rails::MailersControlller#preview. Here, you can see it calling ActionMailer::Preview#call and just passing the name of the "email" (ie: the appropriate method in the preview).