Skip to content

Instantly share code, notes, and snippets.

@lengarvey
Created June 29, 2014 11:28
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save lengarvey/fa2c9bd6cdbeba96526a to your computer and use it in GitHub Desktop.
Save lengarvey/fa2c9bd6cdbeba96526a to your computer and use it in GitHub Desktop.
Enabling Rails 4.1 mail previews in staging (or any other env besides development)
# put this in your staging.rb file. Obviously you'll need more config than this it's just an example.
Rails.application.configure do
config.action_mailer.preview_path ||= defined?(Rails.root) ? "#{Rails.root}/test/mailers/previews" : nil
config.autoload_paths += [config.action_mailer.preview_path]
routes.append do
get '/rails/mailers' => "rails/mailers#index"
get '/rails/mailers/*path' => "rails/mailers#preview"
end
end
# assuming you've configured your mail previews in your test folder this will work fine
@jmcnevin
Copy link

jmcnevin commented Jul 3, 2014

Thanks for this!

@kenn
Copy link

kenn commented Nov 25, 2014

As of 4.1.8 we also needed this in staging.rb:

class ::Rails::MailersController
  def local_request?
    true
  end
end

@dtaniwaki
Copy link

It seems we need to include url_helpers manually.

class ::Rails::MailersController
+  include Rails.application.routes.url_helpers
  def local_request?
    true
  end
end

@dtaniwaki
Copy link

It seems we need to include url_helpers manually. Otherwise, the mailer preview index throws arguments passed to url_for can't be handled. Please require routes or provide your own implementation.

class ::Rails::MailersController
  include Rails.application.routes.url_helpers

  def local_request?
    true
  end
end

@armchairdj
Copy link

As of Rails 4.2 you can do all of this with a config option, obviating the need for this workaround.

Just add this to config/<environment_name).rb

  config.action_mailer.preview_path  = "#{Rails.root}/whatever"
  config.action_mailer.show_previews = true

@romikoops
Copy link

@armchairdj Thank you!

@TylerRick
Copy link

How do you make it so it is only accessible to admin users? The authorization methods that would normally be available (from Devise in my case) are not available since Rails::MailersController is not a subclass of ApplicationController.

@TylerRick
Copy link

I spoke too soon. The methods defined in ApplicationController are not available in Rails::MailersController, but the methods from devise (current_user) are available here (thanks to https://gist.github.com/gabeodess/46d8e0dfcd8960defc4f for hinting me in the right direction).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment