mhfs / devise-async

Send Devise's emails in background. Supports Resque, Sidekiq, Delayed::Job and QueueClassic.
MIT License
575 stars 110 forks source link

Change ActionMailer::Base.default_url_options on runtime #66

Open mattherick opened 9 years ago

mattherick commented 9 years ago

Hi!

I have a problem with changing the ActionMailer::Base.default_url_options = {:host => host} on runtime.

My environment

rails (4.2.0)
sidekiq (3.3.1)
devise (3.4.1)
devise-async (0.9.0)

I have a before_action in my application_controller

class ApplicationController < ActionController::Base

  before_action :set_action_mailer_default_url_options

  private

  def set_action_mailer_default_url_options
    host = "some-logic-to-get-the-correct-host
    ActionMailer::Base.default_url_options = {:host => host}
  end

end

Now if I want to reset my password I always get the default url options, which I have specified in the environments file. If I remove the default_url_options from my environments I get an error in my sidekiq logs.

ActionView::Template::Error: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true

The host is always set correctly in the controller. I already debugged that. It seems that the host is not passed to sidekiq.. any idea how I can fix this?

cmcguff commented 9 years ago

I have the same issue - I have worked around it with a somewhat nasty fix. Basically I am only using devise to send password reset emails, so have created a custom mailer based on https://github.com/plataformatec/devise/wiki/How-To:-Use-custom-mailer

Then I am overriding the reset_password_instructions and setting the host there:

def reset_password_instructions(record, token, opts={})
    ActionMailer::Base.default_url_options[:host] = Account.find(record.account_id).full_domain
    super
end  
farkerhaiku commented 9 years ago

Same problem. everything was fine until i installed this gem and then it all broke. I just started looking into it. Stack overflow has a bunch of questions about the same thing that are all unanswered. Host is just not passed via this gem unless it is hard coded in config/environments/<environment>.rb