Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the routes I need to set up to preview emails using 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

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?

like image 829
sergserg Avatar asked Sep 30 '14 21:09

sergserg


1 Answers

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!

like image 126
curtp Avatar answered Oct 27 '22 09:10

curtp