使用Rails 4.1 ActionMailer :: Preview预览电子邮件需要设置哪些路径? [英] What are the routes I need to set up to preview emails using Rails 4.1 ActionMailer::Preview?

查看:104
本文介绍了使用Rails 4.1 ActionMailer :: Preview预览电子邮件需要设置哪些路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class UserPreview < ActionMailer::Preview
  # Accessible from http://localhost:3000/rails/mailers/notifier/welcome_email
  def welcome_email
    UserMailer.welcome_email(User.first)
  end
end

我有一个使用Ruby on Rails 4.1的简单邮件预览。

I have this simple mailer preview using Ruby on Rails 4.1.

如果我将其注释掉,则我的 routes.rb 文件中的所有路由并仅保留此内容,邮件预览器将起作用:

If I comment out, all of the routes in my routes.rb file and leave only this, the mailer preview works:

MyTestApp::Application.routes.draw do

end

显然,在默认的Rails邮件预览之前,我的权利之一已被使用。

So obviously one of my rights is getting used before the default Rails one for mailer previews.

我需要在路由rb文件中键入什么?

What do I need to type into the routes rb file?

推荐答案

我知道这是一个老问题,但我想通了

I know this is an old question, but figured I'd post an answer anyway.

我猜你在route.rb文件的末尾附近有一条与此类似的路由:

I'm guessing you have a route similar to this near the end of your routes.rb file:

match '/:controller(/:action(/:id))'

这是一条一网打尽的路线。 rails代码将邮件预览路径附加到路径的末尾,因此由于全部捕获路径而无法到达。

That is a 'catch all' route. The rails code appends the mailer preview routes to the end of the routes, so they are never reached due to the 'catch all' route.

听起来像是捕获所有的路线都可能在Rails 5.0中淘汰?审查路线可能是个好主意,这样就不需要全部了解。这是指向某个问题的链接,有人提到全部捕获已在某个时候退役: https://github.com。 com / rails / rails / issues / 15600

It sounds like the 'catch all' route may be retired in rails 5.0? It is probably a good idea to review your routes so you don't need a 'catch all'. Here is a link to the issue where someone mentions the 'catch all' is being retired at some point: https://github.com/rails/rails/issues/15600

所以,这是解决方法。 使用后果自负!

So, here is the fix. Use at your own risk!

在全部接收之前插入邮寄路线。

Insert the mailer routes before your 'catch all'.

    get '/rails/mailers' => "rails/mailers#index"
    get '/rails/mailers/*path' => "rails/mailers#preview"

这将使您的邮件工作,并且全部收录继续工作。现在,这是一个完整的 hack ,只有在您能够解决根本问题之前,才可以使用它,从而消除了全部捕获路线的需要。

That will allow your mailers to work and your 'catch all' will continue working. Now, this is a complete hack which should only be used until you're able to fix the root issue, which is eliminating the need for the 'catch all' route.

我确实在rails的问题列表中找到了以下内容,看起来已被接受并合并。不知道它是哪个版本,但似乎他们已经更新了邮件预览程序代码以添加路由而不是附加路由。

I did find the following in the issues list for rails, which looks like has been accepted and merged. Not sure what version it is in, but it seems like they have updated the mailer preview code to prepend the routes instead of appending them.

https://github.com/rails/rails/pull/17896/files

祝你好运!

这篇关于使用Rails 4.1 ActionMailer :: Preview预览电子邮件需要设置哪些路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆