简体   繁体   中英

Rails - URL helpers not working in mailers

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, ie my_helper_url

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

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.

In the mailer add

helper :my

or the helper you need

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

Enjoy

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

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/

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.

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

您需要使用my_helper_url

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM