What are the routes I need to set up to preview emails using Rails 4.1 ActionMailer::Preview?

人走茶凉 提交于 2019-12-08 15:59:05

问题


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

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

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

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

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


回答1:


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

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

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

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.

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"

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.

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

Good luck!



来源:https://stackoverflow.com/questions/26130130/what-are-the-routes-i-need-to-set-up-to-preview-emails-using-rails-4-1-actionmai

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!