Search code examples
ruby-on-railsdevise

Rails dynamic default_url_options setting for devise


The logic for url_options is set within a Rails 7 application logic as a dynamic value, thus the configuration setting is disabled # config.action_mailer.default_url_options = { host: 'some_domain' }

routes are amended to allow altering of the passwords controller:

devise_for :users, controllers: { passwords: 'users/passwords' }

And the controller action is amended to define the default_url. The new action also injects a host parameter. Upon checking

   def create
 Rails.logger.info params[:user][:host]
 Rails.logger.info request.host
     default_url_options = { host: request.host }
     super
   end

the logs indicate for both logger items localhost. Yet the action does not complete as a couple of issues are encountered:

DEPRECATION WARNING: Initializing Mail::Sendmail with :arguments of type String is deprecated.  
Instead ensure :arguments is an array of strings, e.g. ["-i", "-t"]  
(called from create at /Users/deploy/r/some/app/controllers/users/passwords_controller.rb:16)
[...]
ActionView::Template::Error (Missing host to link to! Please provide the :host parameter,  
set default_url_options[:host], or set :only_path to true):

Why is the action not handling the given parameters or request info?
What is mistaken in the above approach?


Solution

  • Unexpectedly, after having toiled with different avenues, the following ended up working:

    class Users::PasswordsController < Devise::PasswordsController
    
       def create
         Rails.logger.info request.host  #  just checking ;-)
         #     default_url_options = { host: request.host }  # no go
         #     default_url_options[:host] = request.host   # no go
         Rails.application.routes.default_url_options[:host] = request.host
         super
       end
    

    This however implies that every mailer's create action establish the default_url_options before launching.