Rails - URL helpers not working in mailers
Asked Answered
P

8

16

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

Penetration answered 21/7, 2013 at 18:3 Comment(1)
Please accept one of the answers that helped you. :)Collywobbles
C
17

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
Collywobbles answered 30/1, 2017 at 17:44 Comment(2)
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: #34573587Perfect
@Perfect 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 InvoiceMailerBarehanded
E
6

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

Eccrine answered 1/10, 2018 at 0:13 Comment(0)
D
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.

Delossantos answered 22/11, 2013 at 15:56 Comment(0)
A
1

In the mailer add

helper :my

or the helper you need

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

Enjoy

Anisometropia answered 21/7, 2013 at 18:9 Comment(1)
thanks, but I am tryin to use Rails route helpers. I suppose you are talking about helpers in app/helpersPenetration
M
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

Mummy answered 21/7, 2013 at 22:15 Comment(1)
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/...Penetration
H
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.

Helotism answered 12/1, 2016 at 15:45 Comment(0)
B
-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/

Bloodmobile answered 24/11, 2014 at 11:9 Comment(1)
link returns 404...Parget
H
-2

You need to use the my_helper_url

Handwoven answered 21/7, 2013 at 21:30 Comment(11)
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post.Musicology
WTF is this comment @michaelb958 ?Handwoven
I got here via the Low Quality Posts queue, 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.Musicology
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.Handwoven
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.)Musicology
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"... ;)Handwoven
hehehe, I haven't had seen a fight here.Penetration
O @juanpastas since you're here, can you confirm my suspicion? And if I am not right paste WHERE this error happens?Handwoven
when trying to run console, or server,this is, when loading rails environment.Penetration
I can use helper when changing code at runtime... I am clueless why this happensPenetration
and can you give the full log?Handwoven

© 2022 - 2024 — McMap. All rights reserved.