在非开发层上预览邮件程序

时间:2016-08-28 03:35:49

标签: ruby-on-rails actionmailer railtie

我在spec/mailer/previews下有几个邮件预览。在development我可以查看/rails/mailers/下的所有预览。但是,默认情况下,此功能在其他环境中不存在。

我想在staging环境中启用它并从this post here开始排队。

我做了以下更改 -

配置/ routes.rb中

# Add the routes manually
if Rails.env.staging?
  get "/rails/mailers" => "rails/mailers#index"
  get "/rails/mailers/*path" => "rails/mailers#preview"
end

配置/环境/ staging.rb

Rails.application.configure do
  # Define the mailer preview path
  config.action_mailer.preview_path = "spec/mailers/previews"

  # Specifically add that path and all files under it to the autoload paths
  config.autoload_paths = Dir["#{config.root}/#{config.action_mailer.preview_path}/**"]
end

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

  # Override the method just for this controller so `MailersController` thinks
  # all requests are local.
  def local_request?
    true
  end
end

然而,在暂存时,我在尝试加载/rails/mailers页面时收到以下错误 -

LoadError (No such file to load -- spec/mailers/previews/admin_mailer_preview):

奇怪的是......那个文件肯定存在。当我检查分段上的自动加载路径时,该文件肯定在数组/列表中。

有关此处可能发生的事情的任何想法,或者我应该如何揭露该端点?

谢谢!

3 个答案:

答案 0 :(得分:5)

It depends on what Rails version are you running, but if you are on 4.2+ adding these lines to staging.rb should help:

config.action_mailer.show_previews = true
config.consider_all_requests_local = true

答案 1 :(得分:2)

另一个选择是使用像https://mailtrap.io/这样的服务 并获得有关电子邮件的更多有趣信息,例如垃圾邮件和响应性分析 - 我发现它是我的暂存环境的最佳选择。

答案 2 :(得分:1)

拥有consider_all_requests_local = true或修补local_request?可能是安全问题。这是我们使用的解决方案,它使用身份验证只允许管理员访问预览:

# in any enviroment
config.action_mailer.show_previews = true

# in initializers/mailer_previews.rb
# (use your own authentication methods)

# Allow admins to see previews if show_previews enabled.
# It does not affect dev env, as this setting is nil there.
if Rails.application.config.action_mailer.show_previews
  Rails::MailersController.prepend_before_action do
    authenticate_user!
    head :forbidden unless current_user.admin?
  end
end

# If you use rspec-rails, it makes rails use spec/mailers/previews
# as previews path. Most likely you don't have rspec-rails on
# staging/production, so need to add this to application.rb:
#
# Make previews available in environments without rspec-rails.
config.action_mailer.preview_path = Rails.root.join('spec', 'mailers', 'previews')

# Bonus: specs. Update with your `sign_in` implementation
# and have `config.action_mailer.show_previews = true` in test.rb
RSpec.describe Rails::MailersController do
  shared_examples 'visible to admin only' do
    it { should redirect_to new_user_session_path }

    context 'for signed in user' do
      sign_in { create(:user) }
      it { should be_forbidden }

      context 'admin' do
        let(:current_user) { create(:user, admin: true) }
        it { should be_ok }
      end
    end
  end

  describe '#index' do
    subject { get '/rails/mailers' }
    include_examples 'visible to admin only'
  end

  describe '#preview' do
    subject { get '/rails/mailers/devise/mailer') }
    include_examples 'visible to admin only'
  end
end