【问题标题】:Previewing Mailers on non-development tiers预览非开发层上的邮件程序
【发布时间】:2016-08-28 03:35:49
【问题描述】:

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

我想在staging 环境中启用它,并从this post here 获取队列。

我做了以下更改 -

config/routes.rb

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

config/environments/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):

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

对这里可能发生的事情有什么想法,或者我应该如何公开那个端点?

谢谢!

【问题讨论】:

  • 你试过config.action_mailer.preview_path = "#{Rails.root}/spec/mailers/previews"
  • 做到了!这么愚蠢的事情-谢谢:)

标签: ruby-on-rails actionmailer railtie


【解决方案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') && response }
    include_examples 'visible to admin only'
  end

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

【讨论】:

  • 非常感谢!很方便
  • 在我的情况下,我需要一个不在同一个命名空间中但能够通过 gist.github.com/rromanchuk/486f6fbd4fec28c8fa69d36220795343 解决的问题
  • before_action 以undefined local variable or method 'current_user' for #<Rails::MailersController:0x00007f88eda2e8b0> 失败。我替换您的authenticate_user! 行也是如此。在我看来,此代码仅适用于普通控制器,而不适用于邮件控制器。
  • current_user 是 devise gem 中返回登录用户的助手。设计将其添加到此控制器。您需要使用自己的身份验证实现并替换此检查。
【解决方案2】:

这取决于您运行的 Rails 版本,但如果您使用的是 4.2+,则将这些行添加到 staging.rb 应该会有所帮助:

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

【讨论】:

    【解决方案3】:

    另一种选择是使用https://mailtrap.io/ 之类的服务 并且还可以获得一些关于电子邮件的更有趣的信息,例如垃圾邮件和响应分析 - 我发现它是我的暂存环境的最佳选择。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-28
      • 2012-12-15
      • 2019-03-22
      • 2021-11-12
      相关资源
      最近更新 更多