I'm having a hard time figuring out the "Rails Way" to add a email confirmation URL to a mailer.
I'm opting not to do this purely RESTfully because, well, it's difficult with text email because they can't PUT requests.
so here's my routes.rb: get 'confirm/:id' => 'Confirmations#confirm'
and in my mailer I'd like to put email_confirm_url(@user.email_token) where I want the URL to occur.
I created a helper:
#app/helpers/confirmations_helper.rb
module ConfirmationsHelper
  def email_confirm_url(token)
    "/confirm/#{token}"
  end  
end
this all works, sort of, except when I call email_confirm_url(@user.email_token)…
I literally get: "/confirm/abcdefg…" 
When what I want is: http://myhostname/confirm/abcdefg… 
Or in development: http://localhost:3000/confirm/abcdefg…
How can I make my URL helper behave more like the built in <resource>_path and <resource>_url helpers in Rails? though realistically I suppose I really only need _url.
#Edit: I have this in my environment config:
#config/environments/development.rb
...
config.action_mailer.default_url_options = { :host => "localhost:3000" }
                I recently wrote a helper to convert my _path method into a _url method.
Rails uses ActionDispatch::Http::URL.full_url_for to produce the _url methods, and passes in Rails.application.routes.default_url_options to set the host, port, and protocol.
This means you can generate a URL from a given path with
ActionDispatch::Http::URL.full_url_for(Rails.application.routes.default_url_options.merge(path: path))
My work in progress helper looks like:
def self.url_helper(route_name)
  define_method("#{route_name}_url") do |*args|
    path = public_send(:"#{route_name}_path", *args)
    options = Rails.application.routes.default_url_options.merge(path: path)
    ActionDispatch::Http::URL.full_url_for(options)
  end
end
This could then be used in combination with your path helper to build an email_confirm_url method:
url_helper :email_confirm
def email_confirm_path(token)
  "/confirm/#{token}"
end 
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With