Devise Error: NameError (uninitialized constant Unlock)
I recently started getting complaints that locked users cannot reset their accounts from the email I am sending.
I'm getting
NameError (uninitialized constant Unlock):
config/initializers/quiet_assets.rb:6:in `call_with_quiet_assets'
Nothing has changed on my routesr and nothing has changed in my links... what could be causing this error message and how can I fix it?
quiet_assets.rb:
Rails.application.assets.logger = Logger.new('log/logger.txt')
Rails::Rack::Logger.class_eval do
def call_with_quiet_assets(env)
previous_level = Rails.logger.level
Rails.logger.level = Logger::ERROR if env['PATH_INFO'].index("/assets/") == 0
call_without_quiet_assets(env).tap do
Rails.logger.level = previous_level
end
end
alias_method_chain :call, :quiet_assets
end
Stacktrace:
INFO Started GET "/users/unlock?unlock_token=CzpxHwV5kL7EyDZb32Ex" for 127.0.0.1 at 2018-11-22 21:08:56 +0200
INFO Processing by Devise::UnlocksController#show as HTML
INFO Parameters: {"unlock_token"=>"CzpxHwV5kL7EyDZb32Ex"}
INFO -- store_location: /users/unlock?unlock_token=CzpxHwV5kL7EyDZb32Ex
INFO Completed 500 Internal Server Error in 8.1ms
FATAL NameError (uninitialized constant Unlock):
config/initializers/quiet_assets.rb:6:in `call_with_quiet_assets'
INFO Rendered /Users/david/.rvm/gems/ruby-1.9.3-p551/gems/actionpack-3.2.17/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.7ms)
INFO Rendered /Users/david/.rvm/gems/ruby-1.9.3-p551/gems/actionpack-3.2.17/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (0.8ms)
Controller
require 'white_label_utils'
include ERB::Util
#Need to include these helpers or image_path won't work
include Sprockets::Helpers::RailsHelper
include Sprockets::Helpers::IsolatedHelper
include ActionView::Helpers::AssetTagHelper
class CustomMailer < Devise::Mailer
include Devise::Mailers::Helpers
include Devise::Controllers::UrlHelpers # Optional. eg. `confirmation_url`
helper :application # gives access to all helpers defined within `application_helper`.
default from: 'no-reply@ourcompany.com'
# Sends a simple email (without attachments and call to action button)
# to - array of recipients
# subject - subject title of email
# message - body of email
# section - section used for white labeling purposes
# file_attachments - array of files attachments to be included in email. eg. [{name: 'myFileName.csv', content: fileData}, {name: 'anotherFileName.zip', content: anotherFileData}] Empty by default
def simple_email(to, subject, message, section = nil, file_attachments = )
get_whitelabel_details(section)
set_email_global_params(to, subject, message)
# if there are files, attach them
file_attachments = if file_attachments.nil?
if file_attachments.length > 0
file_attachments.each do |file|
attachments["#{file[:name]}"] = file[:content]
end
end
mail(to: to, subject: @subject, from: @whitelabel.email_from).delivery_method.settings.merge!(Dynamic::Application.config.send_grid_smtp_settings)
end
# Sends a devise invitation_instructions email located under app/views/user/mailer/invitation_instructions.html
def invitation_instructions(record, opts = {})
to = record.email
begin
section = record.publisher.sections.first
rescue => e
Rails.logger.warn "Could not find any sections associated with user #{record}"
section = nil
end
initialize_from_record(record)
get_whitelabel_details(section)
set_email_global_params(to, invitation_email_subject, I18n.t("devise.mailer.invitation_instructions.message", invitee_name: record.first_name.capitalize, inviter_name: @resource.invited_by.is_dy_admin ? @resource.invited_by.last_name.capitalize : @resource.invited_by.full_name, product_name: @whitelabel.product_name))
uri = URI.parse(edit_invitations_url({invitation_token: @resource.invitation_token}))
@cta_text = I18n.t("devise.mailer.invitation_instructions.call_to_action_text")
@cta_link = whitelabel_links(uri)
super record, opts
end
# Sends a devise unlock_instructions email located under app/views/user/mailer/unlock_instructions.html
def unlock_instructions(record, opts = {})
to = record.email
begin
section = record.publisher.sections.first
rescue => e
Rails.logger.warn "Could not find any sections associated with user #{record}"
section = nil
end
initialize_from_record(record)
get_whitelabel_details(section)
set_email_global_params(to, I18n.t("devise.mailer.unlock_instructions.subject"), I18n.t("devise.mailer.unlock_instructions.message"))
uri = URI.parse(unlock_url(@resource, :unlock_token => @resource.unlock_token))
@cta_text = I18n.t("devise.mailer.unlock_instructions.call_to_action_text")
@cta_link = whitelabel_links(uri)
super record, opts
end
# Sends a devise reset_password_instruction email located under app/views/user/mailer/reset_password_instructions.html
def reset_password_instructions(record, token = nil, opts = {})
to = record.email
begin
section = record.publisher.sections.first
rescue => e
Rails.logger.warn "Could not find any sections associated with user #{record}"
section = nil
end
initialize_from_record(record)
get_whitelabel_details(section)
set_email_global_params(to, I18n.t("devise.mailer.reset_password_instructions.subject"), I18n.t("devise.mailer.reset_password_instructions.message"))
uri = URI.parse(edit_password_url(@resource, {reset_password_token: @resource.reset_password_token}))
@cta_text = I18n.t("devise.mailer.reset_password_instructions.call_to_action_text")
@cta_link = whitelabel_links(uri)
super record, opts
end
private
def devise_mail(record, action, opts={})
initialize_from_record(record)
(mail headers_for(action, opts)).delivery_method.settings.merge!(Dynamic::Application.config.send_grid_smtp_settings)
end
# Customize the subject and sender display name by the white-label profile
def headers_for(action, opts)
headers = {:from => @email_from,
:reply_to => @email_from}
super.merge!(headers)
end
# Overrides the default subject line for devise emails (reset_password_instructions, invitation_instructions, etc)
def subject_for(key)
return super unless key.to_s == 'invitation_instructions'
invitation_email_subject
end
# Gets the whitelabel details associated with the section
def get_whitelabel_details(section)
@section = section.blank? ? err_message('section') : section
begin
@whitelabel = WhiteLabelUtils::get_profile(nil, section.site.publisher)
rescue => e
Rails.logger.warn "Could not determine WhiteLabel profile when sending email"
@whitelabel = WhiteLabelUtils::get_profile(nil, nil)
end
end
# Validates the existence of parameters and assigns them to global vars that will be used in the email template itself
def set_email_global_params(to, subject, message)
@errors = nil
@to = to.blank? ? err_message('to') : to
@subject = subject.blank? ? err_message('subject') : subject.slice(0, 1).capitalize + subject.slice(1..-1).chomp('.') #remove trailing period, we add this in the template so this avoids duplicates
@message = message.blank? ? err_message('message') : message.slice(0, 1).capitalize + message.slice(1..-1).chomp('.') #remove trailing period, we add this in the template so this avoids duplicates
@email_from = @whitelabel.email_from
@reply_to = @whitelabel.reply_to
@introduction = create_introduction(to)
unless @errors.blank?
raise @errors
end
end
def invitation_email_subject
I18n.t("devise.mailer.invitation_instructions.subject", product_name: @whitelabel.product_name)
end
# Receives a generic url and replaces it with whitelabelled domains
def whitelabel_links uri
"#{@whitelabel.root_url_info[:protocol]}://#{@whitelabel.root_url_info[:host]}#{uri.path}?#{uri.query}"
# rendered as https://companydomain.com/users/unlock?unlock_token=TOKENVALUE
end
# Searches the system for a user with 'email_address' and returns
# a personalized introduction with the user's first name otherwise
# returns a generic introduction with the wording 'Dear User'
def create_introduction email_address
user = User.where(email: email_address).first
"#{I18n.t("dy.common.general.hi")}#{user.nil? ? '' : " #{user.first_name.capitalize}"},"
end
def err_message(val)
@errors = @errors.blank? ? '<' + val + '> field can not be empty' : @errors + 'n<' + val + '> field can not be empty'
end
end
The route being called is https://example.com/users/unlock?unlock_token=USERS_TOKEN
and the controller is from devise unlockscontroller
class Devise::UnlocksController < DeviseController
prepend_before_filter :require_no_authentication
# GET /resource/unlock/new
def new
build_resource({})
end
# POST /resource/unlock
def create
self.resource = resource_class.send_unlock_instructions(resource_params)
if successfully_sent?(resource)
respond_with({}, :location => after_sending_unlock_instructions_path_for(resource))
else
respond_with(resource)
end
end
# GET /resource/unlock?unlock_token=abcdef
def show
self.resource = resource_class.unlock_access_by_token(params[:unlock_token])
if resource.errors.empty?
set_flash_message :notice, :unlocked if is_navigational_format?
respond_with_navigational(resource){ redirect_to after_unlock_path_for(resource) }
else
respond_with_navigational(resource.errors, :status => :unprocessable_entity){ render :new }
end
end
protected
# The path used after sending unlock password instructions
def after_sending_unlock_instructions_path_for(resource)
new_session_path(resource) if is_navigational_format?
end
# The path used after unlocking the resource
def after_unlock_path_for(resource)
new_session_path(resource) if is_navigational_format?
end
end
ruby-on-rails ruby devise
|
show 5 more comments
I recently started getting complaints that locked users cannot reset their accounts from the email I am sending.
I'm getting
NameError (uninitialized constant Unlock):
config/initializers/quiet_assets.rb:6:in `call_with_quiet_assets'
Nothing has changed on my routesr and nothing has changed in my links... what could be causing this error message and how can I fix it?
quiet_assets.rb:
Rails.application.assets.logger = Logger.new('log/logger.txt')
Rails::Rack::Logger.class_eval do
def call_with_quiet_assets(env)
previous_level = Rails.logger.level
Rails.logger.level = Logger::ERROR if env['PATH_INFO'].index("/assets/") == 0
call_without_quiet_assets(env).tap do
Rails.logger.level = previous_level
end
end
alias_method_chain :call, :quiet_assets
end
Stacktrace:
INFO Started GET "/users/unlock?unlock_token=CzpxHwV5kL7EyDZb32Ex" for 127.0.0.1 at 2018-11-22 21:08:56 +0200
INFO Processing by Devise::UnlocksController#show as HTML
INFO Parameters: {"unlock_token"=>"CzpxHwV5kL7EyDZb32Ex"}
INFO -- store_location: /users/unlock?unlock_token=CzpxHwV5kL7EyDZb32Ex
INFO Completed 500 Internal Server Error in 8.1ms
FATAL NameError (uninitialized constant Unlock):
config/initializers/quiet_assets.rb:6:in `call_with_quiet_assets'
INFO Rendered /Users/david/.rvm/gems/ruby-1.9.3-p551/gems/actionpack-3.2.17/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.7ms)
INFO Rendered /Users/david/.rvm/gems/ruby-1.9.3-p551/gems/actionpack-3.2.17/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (0.8ms)
Controller
require 'white_label_utils'
include ERB::Util
#Need to include these helpers or image_path won't work
include Sprockets::Helpers::RailsHelper
include Sprockets::Helpers::IsolatedHelper
include ActionView::Helpers::AssetTagHelper
class CustomMailer < Devise::Mailer
include Devise::Mailers::Helpers
include Devise::Controllers::UrlHelpers # Optional. eg. `confirmation_url`
helper :application # gives access to all helpers defined within `application_helper`.
default from: 'no-reply@ourcompany.com'
# Sends a simple email (without attachments and call to action button)
# to - array of recipients
# subject - subject title of email
# message - body of email
# section - section used for white labeling purposes
# file_attachments - array of files attachments to be included in email. eg. [{name: 'myFileName.csv', content: fileData}, {name: 'anotherFileName.zip', content: anotherFileData}] Empty by default
def simple_email(to, subject, message, section = nil, file_attachments = )
get_whitelabel_details(section)
set_email_global_params(to, subject, message)
# if there are files, attach them
file_attachments = if file_attachments.nil?
if file_attachments.length > 0
file_attachments.each do |file|
attachments["#{file[:name]}"] = file[:content]
end
end
mail(to: to, subject: @subject, from: @whitelabel.email_from).delivery_method.settings.merge!(Dynamic::Application.config.send_grid_smtp_settings)
end
# Sends a devise invitation_instructions email located under app/views/user/mailer/invitation_instructions.html
def invitation_instructions(record, opts = {})
to = record.email
begin
section = record.publisher.sections.first
rescue => e
Rails.logger.warn "Could not find any sections associated with user #{record}"
section = nil
end
initialize_from_record(record)
get_whitelabel_details(section)
set_email_global_params(to, invitation_email_subject, I18n.t("devise.mailer.invitation_instructions.message", invitee_name: record.first_name.capitalize, inviter_name: @resource.invited_by.is_dy_admin ? @resource.invited_by.last_name.capitalize : @resource.invited_by.full_name, product_name: @whitelabel.product_name))
uri = URI.parse(edit_invitations_url({invitation_token: @resource.invitation_token}))
@cta_text = I18n.t("devise.mailer.invitation_instructions.call_to_action_text")
@cta_link = whitelabel_links(uri)
super record, opts
end
# Sends a devise unlock_instructions email located under app/views/user/mailer/unlock_instructions.html
def unlock_instructions(record, opts = {})
to = record.email
begin
section = record.publisher.sections.first
rescue => e
Rails.logger.warn "Could not find any sections associated with user #{record}"
section = nil
end
initialize_from_record(record)
get_whitelabel_details(section)
set_email_global_params(to, I18n.t("devise.mailer.unlock_instructions.subject"), I18n.t("devise.mailer.unlock_instructions.message"))
uri = URI.parse(unlock_url(@resource, :unlock_token => @resource.unlock_token))
@cta_text = I18n.t("devise.mailer.unlock_instructions.call_to_action_text")
@cta_link = whitelabel_links(uri)
super record, opts
end
# Sends a devise reset_password_instruction email located under app/views/user/mailer/reset_password_instructions.html
def reset_password_instructions(record, token = nil, opts = {})
to = record.email
begin
section = record.publisher.sections.first
rescue => e
Rails.logger.warn "Could not find any sections associated with user #{record}"
section = nil
end
initialize_from_record(record)
get_whitelabel_details(section)
set_email_global_params(to, I18n.t("devise.mailer.reset_password_instructions.subject"), I18n.t("devise.mailer.reset_password_instructions.message"))
uri = URI.parse(edit_password_url(@resource, {reset_password_token: @resource.reset_password_token}))
@cta_text = I18n.t("devise.mailer.reset_password_instructions.call_to_action_text")
@cta_link = whitelabel_links(uri)
super record, opts
end
private
def devise_mail(record, action, opts={})
initialize_from_record(record)
(mail headers_for(action, opts)).delivery_method.settings.merge!(Dynamic::Application.config.send_grid_smtp_settings)
end
# Customize the subject and sender display name by the white-label profile
def headers_for(action, opts)
headers = {:from => @email_from,
:reply_to => @email_from}
super.merge!(headers)
end
# Overrides the default subject line for devise emails (reset_password_instructions, invitation_instructions, etc)
def subject_for(key)
return super unless key.to_s == 'invitation_instructions'
invitation_email_subject
end
# Gets the whitelabel details associated with the section
def get_whitelabel_details(section)
@section = section.blank? ? err_message('section') : section
begin
@whitelabel = WhiteLabelUtils::get_profile(nil, section.site.publisher)
rescue => e
Rails.logger.warn "Could not determine WhiteLabel profile when sending email"
@whitelabel = WhiteLabelUtils::get_profile(nil, nil)
end
end
# Validates the existence of parameters and assigns them to global vars that will be used in the email template itself
def set_email_global_params(to, subject, message)
@errors = nil
@to = to.blank? ? err_message('to') : to
@subject = subject.blank? ? err_message('subject') : subject.slice(0, 1).capitalize + subject.slice(1..-1).chomp('.') #remove trailing period, we add this in the template so this avoids duplicates
@message = message.blank? ? err_message('message') : message.slice(0, 1).capitalize + message.slice(1..-1).chomp('.') #remove trailing period, we add this in the template so this avoids duplicates
@email_from = @whitelabel.email_from
@reply_to = @whitelabel.reply_to
@introduction = create_introduction(to)
unless @errors.blank?
raise @errors
end
end
def invitation_email_subject
I18n.t("devise.mailer.invitation_instructions.subject", product_name: @whitelabel.product_name)
end
# Receives a generic url and replaces it with whitelabelled domains
def whitelabel_links uri
"#{@whitelabel.root_url_info[:protocol]}://#{@whitelabel.root_url_info[:host]}#{uri.path}?#{uri.query}"
# rendered as https://companydomain.com/users/unlock?unlock_token=TOKENVALUE
end
# Searches the system for a user with 'email_address' and returns
# a personalized introduction with the user's first name otherwise
# returns a generic introduction with the wording 'Dear User'
def create_introduction email_address
user = User.where(email: email_address).first
"#{I18n.t("dy.common.general.hi")}#{user.nil? ? '' : " #{user.first_name.capitalize}"},"
end
def err_message(val)
@errors = @errors.blank? ? '<' + val + '> field can not be empty' : @errors + 'n<' + val + '> field can not be empty'
end
end
The route being called is https://example.com/users/unlock?unlock_token=USERS_TOKEN
and the controller is from devise unlockscontroller
class Devise::UnlocksController < DeviseController
prepend_before_filter :require_no_authentication
# GET /resource/unlock/new
def new
build_resource({})
end
# POST /resource/unlock
def create
self.resource = resource_class.send_unlock_instructions(resource_params)
if successfully_sent?(resource)
respond_with({}, :location => after_sending_unlock_instructions_path_for(resource))
else
respond_with(resource)
end
end
# GET /resource/unlock?unlock_token=abcdef
def show
self.resource = resource_class.unlock_access_by_token(params[:unlock_token])
if resource.errors.empty?
set_flash_message :notice, :unlocked if is_navigational_format?
respond_with_navigational(resource){ redirect_to after_unlock_path_for(resource) }
else
respond_with_navigational(resource.errors, :status => :unprocessable_entity){ render :new }
end
end
protected
# The path used after sending unlock password instructions
def after_sending_unlock_instructions_path_for(resource)
new_session_path(resource) if is_navigational_format?
end
# The path used after unlocking the resource
def after_unlock_path_for(resource)
new_session_path(resource) if is_navigational_format?
end
end
ruby-on-rails ruby devise
1
Can you show quiet_assets.rb ?
– Neodelf
Nov 22 '18 at 19:28
Updated my post
– Adrian E
Nov 22 '18 at 19:40
1
Which is line 6?
– cd-rum
Nov 22 '18 at 19:44
Could you post full stack trace?
– mrzasa
Nov 22 '18 at 19:46
@mrzasa added stacktrace
– Adrian E
Nov 22 '18 at 19:51
|
show 5 more comments
I recently started getting complaints that locked users cannot reset their accounts from the email I am sending.
I'm getting
NameError (uninitialized constant Unlock):
config/initializers/quiet_assets.rb:6:in `call_with_quiet_assets'
Nothing has changed on my routesr and nothing has changed in my links... what could be causing this error message and how can I fix it?
quiet_assets.rb:
Rails.application.assets.logger = Logger.new('log/logger.txt')
Rails::Rack::Logger.class_eval do
def call_with_quiet_assets(env)
previous_level = Rails.logger.level
Rails.logger.level = Logger::ERROR if env['PATH_INFO'].index("/assets/") == 0
call_without_quiet_assets(env).tap do
Rails.logger.level = previous_level
end
end
alias_method_chain :call, :quiet_assets
end
Stacktrace:
INFO Started GET "/users/unlock?unlock_token=CzpxHwV5kL7EyDZb32Ex" for 127.0.0.1 at 2018-11-22 21:08:56 +0200
INFO Processing by Devise::UnlocksController#show as HTML
INFO Parameters: {"unlock_token"=>"CzpxHwV5kL7EyDZb32Ex"}
INFO -- store_location: /users/unlock?unlock_token=CzpxHwV5kL7EyDZb32Ex
INFO Completed 500 Internal Server Error in 8.1ms
FATAL NameError (uninitialized constant Unlock):
config/initializers/quiet_assets.rb:6:in `call_with_quiet_assets'
INFO Rendered /Users/david/.rvm/gems/ruby-1.9.3-p551/gems/actionpack-3.2.17/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.7ms)
INFO Rendered /Users/david/.rvm/gems/ruby-1.9.3-p551/gems/actionpack-3.2.17/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (0.8ms)
Controller
require 'white_label_utils'
include ERB::Util
#Need to include these helpers or image_path won't work
include Sprockets::Helpers::RailsHelper
include Sprockets::Helpers::IsolatedHelper
include ActionView::Helpers::AssetTagHelper
class CustomMailer < Devise::Mailer
include Devise::Mailers::Helpers
include Devise::Controllers::UrlHelpers # Optional. eg. `confirmation_url`
helper :application # gives access to all helpers defined within `application_helper`.
default from: 'no-reply@ourcompany.com'
# Sends a simple email (without attachments and call to action button)
# to - array of recipients
# subject - subject title of email
# message - body of email
# section - section used for white labeling purposes
# file_attachments - array of files attachments to be included in email. eg. [{name: 'myFileName.csv', content: fileData}, {name: 'anotherFileName.zip', content: anotherFileData}] Empty by default
def simple_email(to, subject, message, section = nil, file_attachments = )
get_whitelabel_details(section)
set_email_global_params(to, subject, message)
# if there are files, attach them
file_attachments = if file_attachments.nil?
if file_attachments.length > 0
file_attachments.each do |file|
attachments["#{file[:name]}"] = file[:content]
end
end
mail(to: to, subject: @subject, from: @whitelabel.email_from).delivery_method.settings.merge!(Dynamic::Application.config.send_grid_smtp_settings)
end
# Sends a devise invitation_instructions email located under app/views/user/mailer/invitation_instructions.html
def invitation_instructions(record, opts = {})
to = record.email
begin
section = record.publisher.sections.first
rescue => e
Rails.logger.warn "Could not find any sections associated with user #{record}"
section = nil
end
initialize_from_record(record)
get_whitelabel_details(section)
set_email_global_params(to, invitation_email_subject, I18n.t("devise.mailer.invitation_instructions.message", invitee_name: record.first_name.capitalize, inviter_name: @resource.invited_by.is_dy_admin ? @resource.invited_by.last_name.capitalize : @resource.invited_by.full_name, product_name: @whitelabel.product_name))
uri = URI.parse(edit_invitations_url({invitation_token: @resource.invitation_token}))
@cta_text = I18n.t("devise.mailer.invitation_instructions.call_to_action_text")
@cta_link = whitelabel_links(uri)
super record, opts
end
# Sends a devise unlock_instructions email located under app/views/user/mailer/unlock_instructions.html
def unlock_instructions(record, opts = {})
to = record.email
begin
section = record.publisher.sections.first
rescue => e
Rails.logger.warn "Could not find any sections associated with user #{record}"
section = nil
end
initialize_from_record(record)
get_whitelabel_details(section)
set_email_global_params(to, I18n.t("devise.mailer.unlock_instructions.subject"), I18n.t("devise.mailer.unlock_instructions.message"))
uri = URI.parse(unlock_url(@resource, :unlock_token => @resource.unlock_token))
@cta_text = I18n.t("devise.mailer.unlock_instructions.call_to_action_text")
@cta_link = whitelabel_links(uri)
super record, opts
end
# Sends a devise reset_password_instruction email located under app/views/user/mailer/reset_password_instructions.html
def reset_password_instructions(record, token = nil, opts = {})
to = record.email
begin
section = record.publisher.sections.first
rescue => e
Rails.logger.warn "Could not find any sections associated with user #{record}"
section = nil
end
initialize_from_record(record)
get_whitelabel_details(section)
set_email_global_params(to, I18n.t("devise.mailer.reset_password_instructions.subject"), I18n.t("devise.mailer.reset_password_instructions.message"))
uri = URI.parse(edit_password_url(@resource, {reset_password_token: @resource.reset_password_token}))
@cta_text = I18n.t("devise.mailer.reset_password_instructions.call_to_action_text")
@cta_link = whitelabel_links(uri)
super record, opts
end
private
def devise_mail(record, action, opts={})
initialize_from_record(record)
(mail headers_for(action, opts)).delivery_method.settings.merge!(Dynamic::Application.config.send_grid_smtp_settings)
end
# Customize the subject and sender display name by the white-label profile
def headers_for(action, opts)
headers = {:from => @email_from,
:reply_to => @email_from}
super.merge!(headers)
end
# Overrides the default subject line for devise emails (reset_password_instructions, invitation_instructions, etc)
def subject_for(key)
return super unless key.to_s == 'invitation_instructions'
invitation_email_subject
end
# Gets the whitelabel details associated with the section
def get_whitelabel_details(section)
@section = section.blank? ? err_message('section') : section
begin
@whitelabel = WhiteLabelUtils::get_profile(nil, section.site.publisher)
rescue => e
Rails.logger.warn "Could not determine WhiteLabel profile when sending email"
@whitelabel = WhiteLabelUtils::get_profile(nil, nil)
end
end
# Validates the existence of parameters and assigns them to global vars that will be used in the email template itself
def set_email_global_params(to, subject, message)
@errors = nil
@to = to.blank? ? err_message('to') : to
@subject = subject.blank? ? err_message('subject') : subject.slice(0, 1).capitalize + subject.slice(1..-1).chomp('.') #remove trailing period, we add this in the template so this avoids duplicates
@message = message.blank? ? err_message('message') : message.slice(0, 1).capitalize + message.slice(1..-1).chomp('.') #remove trailing period, we add this in the template so this avoids duplicates
@email_from = @whitelabel.email_from
@reply_to = @whitelabel.reply_to
@introduction = create_introduction(to)
unless @errors.blank?
raise @errors
end
end
def invitation_email_subject
I18n.t("devise.mailer.invitation_instructions.subject", product_name: @whitelabel.product_name)
end
# Receives a generic url and replaces it with whitelabelled domains
def whitelabel_links uri
"#{@whitelabel.root_url_info[:protocol]}://#{@whitelabel.root_url_info[:host]}#{uri.path}?#{uri.query}"
# rendered as https://companydomain.com/users/unlock?unlock_token=TOKENVALUE
end
# Searches the system for a user with 'email_address' and returns
# a personalized introduction with the user's first name otherwise
# returns a generic introduction with the wording 'Dear User'
def create_introduction email_address
user = User.where(email: email_address).first
"#{I18n.t("dy.common.general.hi")}#{user.nil? ? '' : " #{user.first_name.capitalize}"},"
end
def err_message(val)
@errors = @errors.blank? ? '<' + val + '> field can not be empty' : @errors + 'n<' + val + '> field can not be empty'
end
end
The route being called is https://example.com/users/unlock?unlock_token=USERS_TOKEN
and the controller is from devise unlockscontroller
class Devise::UnlocksController < DeviseController
prepend_before_filter :require_no_authentication
# GET /resource/unlock/new
def new
build_resource({})
end
# POST /resource/unlock
def create
self.resource = resource_class.send_unlock_instructions(resource_params)
if successfully_sent?(resource)
respond_with({}, :location => after_sending_unlock_instructions_path_for(resource))
else
respond_with(resource)
end
end
# GET /resource/unlock?unlock_token=abcdef
def show
self.resource = resource_class.unlock_access_by_token(params[:unlock_token])
if resource.errors.empty?
set_flash_message :notice, :unlocked if is_navigational_format?
respond_with_navigational(resource){ redirect_to after_unlock_path_for(resource) }
else
respond_with_navigational(resource.errors, :status => :unprocessable_entity){ render :new }
end
end
protected
# The path used after sending unlock password instructions
def after_sending_unlock_instructions_path_for(resource)
new_session_path(resource) if is_navigational_format?
end
# The path used after unlocking the resource
def after_unlock_path_for(resource)
new_session_path(resource) if is_navigational_format?
end
end
ruby-on-rails ruby devise
I recently started getting complaints that locked users cannot reset their accounts from the email I am sending.
I'm getting
NameError (uninitialized constant Unlock):
config/initializers/quiet_assets.rb:6:in `call_with_quiet_assets'
Nothing has changed on my routesr and nothing has changed in my links... what could be causing this error message and how can I fix it?
quiet_assets.rb:
Rails.application.assets.logger = Logger.new('log/logger.txt')
Rails::Rack::Logger.class_eval do
def call_with_quiet_assets(env)
previous_level = Rails.logger.level
Rails.logger.level = Logger::ERROR if env['PATH_INFO'].index("/assets/") == 0
call_without_quiet_assets(env).tap do
Rails.logger.level = previous_level
end
end
alias_method_chain :call, :quiet_assets
end
Stacktrace:
INFO Started GET "/users/unlock?unlock_token=CzpxHwV5kL7EyDZb32Ex" for 127.0.0.1 at 2018-11-22 21:08:56 +0200
INFO Processing by Devise::UnlocksController#show as HTML
INFO Parameters: {"unlock_token"=>"CzpxHwV5kL7EyDZb32Ex"}
INFO -- store_location: /users/unlock?unlock_token=CzpxHwV5kL7EyDZb32Ex
INFO Completed 500 Internal Server Error in 8.1ms
FATAL NameError (uninitialized constant Unlock):
config/initializers/quiet_assets.rb:6:in `call_with_quiet_assets'
INFO Rendered /Users/david/.rvm/gems/ruby-1.9.3-p551/gems/actionpack-3.2.17/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.7ms)
INFO Rendered /Users/david/.rvm/gems/ruby-1.9.3-p551/gems/actionpack-3.2.17/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (0.8ms)
Controller
require 'white_label_utils'
include ERB::Util
#Need to include these helpers or image_path won't work
include Sprockets::Helpers::RailsHelper
include Sprockets::Helpers::IsolatedHelper
include ActionView::Helpers::AssetTagHelper
class CustomMailer < Devise::Mailer
include Devise::Mailers::Helpers
include Devise::Controllers::UrlHelpers # Optional. eg. `confirmation_url`
helper :application # gives access to all helpers defined within `application_helper`.
default from: 'no-reply@ourcompany.com'
# Sends a simple email (without attachments and call to action button)
# to - array of recipients
# subject - subject title of email
# message - body of email
# section - section used for white labeling purposes
# file_attachments - array of files attachments to be included in email. eg. [{name: 'myFileName.csv', content: fileData}, {name: 'anotherFileName.zip', content: anotherFileData}] Empty by default
def simple_email(to, subject, message, section = nil, file_attachments = )
get_whitelabel_details(section)
set_email_global_params(to, subject, message)
# if there are files, attach them
file_attachments = if file_attachments.nil?
if file_attachments.length > 0
file_attachments.each do |file|
attachments["#{file[:name]}"] = file[:content]
end
end
mail(to: to, subject: @subject, from: @whitelabel.email_from).delivery_method.settings.merge!(Dynamic::Application.config.send_grid_smtp_settings)
end
# Sends a devise invitation_instructions email located under app/views/user/mailer/invitation_instructions.html
def invitation_instructions(record, opts = {})
to = record.email
begin
section = record.publisher.sections.first
rescue => e
Rails.logger.warn "Could not find any sections associated with user #{record}"
section = nil
end
initialize_from_record(record)
get_whitelabel_details(section)
set_email_global_params(to, invitation_email_subject, I18n.t("devise.mailer.invitation_instructions.message", invitee_name: record.first_name.capitalize, inviter_name: @resource.invited_by.is_dy_admin ? @resource.invited_by.last_name.capitalize : @resource.invited_by.full_name, product_name: @whitelabel.product_name))
uri = URI.parse(edit_invitations_url({invitation_token: @resource.invitation_token}))
@cta_text = I18n.t("devise.mailer.invitation_instructions.call_to_action_text")
@cta_link = whitelabel_links(uri)
super record, opts
end
# Sends a devise unlock_instructions email located under app/views/user/mailer/unlock_instructions.html
def unlock_instructions(record, opts = {})
to = record.email
begin
section = record.publisher.sections.first
rescue => e
Rails.logger.warn "Could not find any sections associated with user #{record}"
section = nil
end
initialize_from_record(record)
get_whitelabel_details(section)
set_email_global_params(to, I18n.t("devise.mailer.unlock_instructions.subject"), I18n.t("devise.mailer.unlock_instructions.message"))
uri = URI.parse(unlock_url(@resource, :unlock_token => @resource.unlock_token))
@cta_text = I18n.t("devise.mailer.unlock_instructions.call_to_action_text")
@cta_link = whitelabel_links(uri)
super record, opts
end
# Sends a devise reset_password_instruction email located under app/views/user/mailer/reset_password_instructions.html
def reset_password_instructions(record, token = nil, opts = {})
to = record.email
begin
section = record.publisher.sections.first
rescue => e
Rails.logger.warn "Could not find any sections associated with user #{record}"
section = nil
end
initialize_from_record(record)
get_whitelabel_details(section)
set_email_global_params(to, I18n.t("devise.mailer.reset_password_instructions.subject"), I18n.t("devise.mailer.reset_password_instructions.message"))
uri = URI.parse(edit_password_url(@resource, {reset_password_token: @resource.reset_password_token}))
@cta_text = I18n.t("devise.mailer.reset_password_instructions.call_to_action_text")
@cta_link = whitelabel_links(uri)
super record, opts
end
private
def devise_mail(record, action, opts={})
initialize_from_record(record)
(mail headers_for(action, opts)).delivery_method.settings.merge!(Dynamic::Application.config.send_grid_smtp_settings)
end
# Customize the subject and sender display name by the white-label profile
def headers_for(action, opts)
headers = {:from => @email_from,
:reply_to => @email_from}
super.merge!(headers)
end
# Overrides the default subject line for devise emails (reset_password_instructions, invitation_instructions, etc)
def subject_for(key)
return super unless key.to_s == 'invitation_instructions'
invitation_email_subject
end
# Gets the whitelabel details associated with the section
def get_whitelabel_details(section)
@section = section.blank? ? err_message('section') : section
begin
@whitelabel = WhiteLabelUtils::get_profile(nil, section.site.publisher)
rescue => e
Rails.logger.warn "Could not determine WhiteLabel profile when sending email"
@whitelabel = WhiteLabelUtils::get_profile(nil, nil)
end
end
# Validates the existence of parameters and assigns them to global vars that will be used in the email template itself
def set_email_global_params(to, subject, message)
@errors = nil
@to = to.blank? ? err_message('to') : to
@subject = subject.blank? ? err_message('subject') : subject.slice(0, 1).capitalize + subject.slice(1..-1).chomp('.') #remove trailing period, we add this in the template so this avoids duplicates
@message = message.blank? ? err_message('message') : message.slice(0, 1).capitalize + message.slice(1..-1).chomp('.') #remove trailing period, we add this in the template so this avoids duplicates
@email_from = @whitelabel.email_from
@reply_to = @whitelabel.reply_to
@introduction = create_introduction(to)
unless @errors.blank?
raise @errors
end
end
def invitation_email_subject
I18n.t("devise.mailer.invitation_instructions.subject", product_name: @whitelabel.product_name)
end
# Receives a generic url and replaces it with whitelabelled domains
def whitelabel_links uri
"#{@whitelabel.root_url_info[:protocol]}://#{@whitelabel.root_url_info[:host]}#{uri.path}?#{uri.query}"
# rendered as https://companydomain.com/users/unlock?unlock_token=TOKENVALUE
end
# Searches the system for a user with 'email_address' and returns
# a personalized introduction with the user's first name otherwise
# returns a generic introduction with the wording 'Dear User'
def create_introduction email_address
user = User.where(email: email_address).first
"#{I18n.t("dy.common.general.hi")}#{user.nil? ? '' : " #{user.first_name.capitalize}"},"
end
def err_message(val)
@errors = @errors.blank? ? '<' + val + '> field can not be empty' : @errors + 'n<' + val + '> field can not be empty'
end
end
The route being called is https://example.com/users/unlock?unlock_token=USERS_TOKEN
and the controller is from devise unlockscontroller
class Devise::UnlocksController < DeviseController
prepend_before_filter :require_no_authentication
# GET /resource/unlock/new
def new
build_resource({})
end
# POST /resource/unlock
def create
self.resource = resource_class.send_unlock_instructions(resource_params)
if successfully_sent?(resource)
respond_with({}, :location => after_sending_unlock_instructions_path_for(resource))
else
respond_with(resource)
end
end
# GET /resource/unlock?unlock_token=abcdef
def show
self.resource = resource_class.unlock_access_by_token(params[:unlock_token])
if resource.errors.empty?
set_flash_message :notice, :unlocked if is_navigational_format?
respond_with_navigational(resource){ redirect_to after_unlock_path_for(resource) }
else
respond_with_navigational(resource.errors, :status => :unprocessable_entity){ render :new }
end
end
protected
# The path used after sending unlock password instructions
def after_sending_unlock_instructions_path_for(resource)
new_session_path(resource) if is_navigational_format?
end
# The path used after unlocking the resource
def after_unlock_path_for(resource)
new_session_path(resource) if is_navigational_format?
end
end
ruby-on-rails ruby devise
ruby-on-rails ruby devise
edited Nov 25 '18 at 9:15
Adrian E
asked Nov 22 '18 at 19:13
Adrian EAdrian E
209516
209516
1
Can you show quiet_assets.rb ?
– Neodelf
Nov 22 '18 at 19:28
Updated my post
– Adrian E
Nov 22 '18 at 19:40
1
Which is line 6?
– cd-rum
Nov 22 '18 at 19:44
Could you post full stack trace?
– mrzasa
Nov 22 '18 at 19:46
@mrzasa added stacktrace
– Adrian E
Nov 22 '18 at 19:51
|
show 5 more comments
1
Can you show quiet_assets.rb ?
– Neodelf
Nov 22 '18 at 19:28
Updated my post
– Adrian E
Nov 22 '18 at 19:40
1
Which is line 6?
– cd-rum
Nov 22 '18 at 19:44
Could you post full stack trace?
– mrzasa
Nov 22 '18 at 19:46
@mrzasa added stacktrace
– Adrian E
Nov 22 '18 at 19:51
1
1
Can you show quiet_assets.rb ?
– Neodelf
Nov 22 '18 at 19:28
Can you show quiet_assets.rb ?
– Neodelf
Nov 22 '18 at 19:28
Updated my post
– Adrian E
Nov 22 '18 at 19:40
Updated my post
– Adrian E
Nov 22 '18 at 19:40
1
1
Which is line 6?
– cd-rum
Nov 22 '18 at 19:44
Which is line 6?
– cd-rum
Nov 22 '18 at 19:44
Could you post full stack trace?
– mrzasa
Nov 22 '18 at 19:46
Could you post full stack trace?
– mrzasa
Nov 22 '18 at 19:46
@mrzasa added stacktrace
– Adrian E
Nov 22 '18 at 19:51
@mrzasa added stacktrace
– Adrian E
Nov 22 '18 at 19:51
|
show 5 more comments
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53436932%2fdevise-error-nameerror-uninitialized-constant-unlock%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53436932%2fdevise-error-nameerror-uninitialized-constant-unlock%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Can you show quiet_assets.rb ?
– Neodelf
Nov 22 '18 at 19:28
Updated my post
– Adrian E
Nov 22 '18 at 19:40
1
Which is line 6?
– cd-rum
Nov 22 '18 at 19:44
Could you post full stack trace?
– mrzasa
Nov 22 '18 at 19:46
@mrzasa added stacktrace
– Adrian E
Nov 22 '18 at 19:51