Rails - URLs that do not work in email programs

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 the mail program:

 include Rails.application.routes.url_helpers def my_mail @my_route = my_helper 

In addition, an easy way, in the template of the mail program:

 = link_to 'asd', my_helper 

But then when I try to start the console, I get:

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

Update

I use the _url helper form, i.e. my_helper_url

+11
ruby-on-rails actionmailer
source share
8 answers

For Rails 5, I included URL assistants in my mail program 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 
+10
source share

I encountered the same problem, but with Concern, I could not use any URL helper in my code, even using Rails.application.routes.url_helpers.administrator_beverages_url directly I received this error:

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

I couldn’t even use the rail console because of this error, the only way I decided to solve it was to use this code in my Concern

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

The problem was that Rails.application.routes.url_helpers was empty at the time of initialization. I do not know how this affects performance, but for my case it is a small application, and I can take a bullet.

+2
source share

In the mailbox add

 helper :my 

or the assistant you need

and it will load the application / helpers / my_helper.rb and includes MyHelper

Enjoy

0
source share

Rail route helpers are located at Rails.application.routes.url_helpers . You should just be able to include Rails.application.routes.url_helpers at the top of your class, although I have not tested this

0
source share

Please take a look at this blog post which explains how you can use Rails.application.routes.url_helpers correctly.

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

0
source share

I ran into this problem while working with a recently added route, which seems to be unavailable in my Mailer. My problem was that I needed to restart all workers so that they would pick up the newly added route. Just leaving this footnote here if someone comes across the same question, it can be difficult to decide if you don't know what is happening.

0
source share

This violated my other routes.

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

This is the wrong way, it will disrupt the application, as the routes are reloaded and the routes will not be available if they reload.

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

The correct answer is to use * _url and not * _path methods in email templates, as explained below in the Rails documentation.

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

0
source share

You need to use my_helper_url

-2
source share

All Articles