15

I tried:

class MyMailer
  def routes
    Rails.application.routes.url_helpers
  end

  def my_mail
    @my_route = routes.my_helper
    ... code omitted 
  end

Also inside mailer:

include Rails.application.routes.url_helpers

def my_mail
  @my_route = my_helper

Also, the simple way, in mailer template:

= link_to 'asd', my_helper

But then when I try to start console I get:

undefined method `my_helper' for #<Module:0x007f9252e39b80> (NoMethodError)

Update

I am using the _url form of the helper, i.e. my_helper_url

sites
  • 21,417
  • 17
  • 87
  • 146

8 Answers8

16

For Rails 5, I included the url helpers into the my mailer file:

# mailers/invoice_mailer.rb
include Rails.application.routes.url_helpers

class InvoiceMailer < ApplicationMailer
    def send_mail(invoice)
        @invoice = invoice
        mail(to: @invoice.client.email, subject: "Invoice Test")
    end
end
Constant Meiring
  • 3,285
  • 3
  • 40
  • 52
  • 1
    This solution can lead to issues. Specifically it can break `link_to` on other templates when deployed on heroku. I imagine this is a bug in rails, but a different workaround may be required, as it was with me. Reference: https://stackoverflow.com/questions/34573087/link-to-does-not-work-in-production-heroku – gdxn96 Sep 29 '18 at 12:45
  • @gdxn96 the problem happens when you start the server in production mode (what heroku does). It won't happen if you put the `include Rails.application.routes.url_helpers` **within** the class `InvoiceMailer` – Raphael Pr Sep 13 '19 at 16:33
5

Doing this broke my other routes.

# mailers/invoice_mailer.rb
include Rails.application.routes.url_helpers

Doing this is not the right way, this will break application as routes are reloading and routes will not be available is those are reloading

module SimpleBackend
    extend ActiveSupport::Concern
    Rails.application.reload_routes!

Right answer is to use *_url and not *_path methods in email templates as explained below in Rails docs.

https://guides.rubyonrails.org/action_mailer_basics.html#generating-urls-in-action-mailer-views

Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
omair azam
  • 520
  • 7
  • 14
2

I ran into the same issue but with a Concern, i was unable to use any of the url helpers in my code even using directly Rails.application.routes.url_helpers.administrator_beverages_url i was getting this error:

undefined method `administrator_beverages_url' for #<Module:0x00000002167158> (NoMethodError)

even unable to use the rails console because of this error the only way i found to solve this was to use this code in my Concern

module SimpleBackend
    extend ActiveSupport::Concern
    Rails.application.reload_routes! #this did the trick
.....

The problem was Rails.application.routes.url_helpers was empty at the moment of the initialization. I don't know the performance implication of using this but for my case this is a small application and i can take the bullet.

Dranes
  • 82
  • 1
  • 4
1

In the mailer add

helper :my

or the helper you need

and it will load app/helpers/my_helper.rb & includes MyHelper

Enjoy

Oz Ben-David
  • 1,589
  • 1
  • 16
  • 26
  • thanks, but I am tryin to use Rails route helpers. I suppose you are talking about helpers in `app/helpers` – sites Jul 21 '13 at 19:10
0

The rails route helpers are in Rails.application.routes.url_helpers. You should just be able to put include Rails.application.routes.url_helpers at the top of your class, though I haven't tested this

John Hinnegan
  • 5,864
  • 2
  • 48
  • 64
  • I tried this, that's weird. What is even weirder is that environment does not load if I use url helper, but I can: 1. comment line, 2. start server for example, 3. uncomment line 4. it works. But this is not going to work for real environment. As a temporal solution I am using static url: www.blahblahblah.com/... – sites Jul 21 '13 at 22:24
0

I came across this issue while working with a newly added route that seemed to be unavailable in my Mailer. My problem was I needed to restart all the workers, so they would pick up the newly added route. Just leaving this footnote in here in case someone runs into the same issue, it can be tricky to solve if you don't know this is happening.

rii
  • 1,578
  • 1
  • 17
  • 22
-1

Please take a look at this blog which explains how you can make use of Rails.application.routes.url_helpers in the right manner.

http://hawkins.io/2012/03/generating_urls_whenever_and_wherever_you_want/

jake
  • 2,371
  • 1
  • 20
  • 31
-2

You need to use the my_helper_url

Mike Szyndel
  • 10,461
  • 10
  • 47
  • 63
  • 2
    This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. – michaelb958--GoFundMonica Jul 21 '13 at 21:48
  • WTF is this comment @michaelb958 ? – Mike Szyndel Jul 21 '13 at 21:51
  • 1
    I got here via the [Low Quality Posts queue](http://stackoverflow.com/review/low-quality-posts/stats), decided there wasn't *quite* enough in this answer, and recommended deletion with that canned comment. You are not required to agree with this; you may edit your post to improve it, or something. – michaelb958--GoFundMonica Jul 21 '13 at 21:54
  • 1
    So next time maybe read the answer and not copy&paste default msg. Guy is calling wrong method, that's all. Doesn't require more explanation. – Mike Szyndel Jul 21 '13 at 21:55
  • I'm not a Rails expert, but I think that (OP) *I am using the `_url` form of the helper* means he's using `my_helper_url`. There is no revision timestamp given, so that info either shipped with the question or was edited in less than 5 minutes after posting. Ergo, this answer (half an hour ago) doesn't really help. (Feel free to disagree here.) – michaelb958--GoFundMonica Jul 21 '13 at 21:59
  • Yeah, sorry, I got angry. Anyway, I already learned that if a person SAYS they have something in code doesn't mean they do until code is pasted here. And in the pasted code I don't see _url "form"... ;) – Mike Szyndel Jul 21 '13 at 22:01
  • hehehe, I haven't had seen a fight here. – sites Jul 21 '13 at 22:05
  • O @juanpastas since you're here, can you confirm my suspicion? And if I am not right paste WHERE this error happens? – Mike Szyndel Jul 21 '13 at 22:08
  • when trying to run console, or server,this is, when loading rails environment. – sites Jul 21 '13 at 22:25
  • I can use helper when changing code at runtime... I am clueless why this happens – sites Jul 21 '13 at 22:26
  • and can you give the full log? – Mike Szyndel Jul 21 '13 at 22:27