I need to adapt forgotten password instructions to handle the subdomain. I followed the instructions on the developer's website to redefine the mail program, controller, and add an auxiliary subdomain, etc. As indicated:
Controllers / password_controller.rb
class PasswordsController < Devise::PasswordsController def create @subdomain = request.subdomain super end end
routes.rb
devise_for :users, controllers: { passwords: 'passwords' }
devise.rb
config.mailer = "UserMailer"
senders / user_mailer.rb
class UserMailer < Devise::Mailer helper :application
view / user_mailer / reset_password_instructions.html.erb
<p>Hello <%= @resource.email %>!</p> <p>Someone has requested a link to change your password. You can do this through the link below.</p> <p><%= link_to 'Change my password', edit_password_url(@resource, :reset_password_token => @resource.reset_password_token, :subdomain => @subdomain) %></p> <p>If you didn't request this, please ignore this email.</p> <p>Your password won't change until you access the link above and create a new one.</p>
helpers / subdomain_helper.rb
module SubdomainHelper def with_subdomain(subdomain) subdomain = (subdomain || "") subdomain += "." unless subdomain.empty? host = Rails.application.config.action_mailer.default_url_options[:host] [subdomain, host].join end def url_for(options = nil) if options.kind_of?(Hash) && options.has_key?(:subdomain) options[:host] = with_subdomain(options.delete(:subdomain)) end super end end
application.rb
config.to_prepare do Devise::Mailer.class_eval do helper :subdomain end end
Now this code works, but it just cannot get the @subdomain value in the view of the mailer. If I replaced @subdomain with a hard-coded string, then the correct address will be emailed so that I know that the code is correct.
How to get the @subdomain instance variable defined in the controller in the mailer view?
Craig mcguff
source share