Rails - URL helpers not working in mailers

sites picture sites · Jul 21, 2013 · Viewed 10.4k times · Source

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

Answer

Constant Meiring picture Constant Meiring · Jan 30, 2017

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