Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: make custom URL helper behave like built in _path & _url helpers

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" }
like image 277
Meltemi Avatar asked Nov 12 '22 13:11

Meltemi


1 Answers

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 
like image 106
James EJ Avatar answered Nov 15 '22 06:11

James EJ