在我的一个项目中,在运行时更改ActionMailer::Base.default_url_options = {:host =>主机}有问题。
安装程序:我有多个子域,它们在后端使用相同的Rails-Application。我想通过Sidekiq队列将我的设计邮件发送给用户。设计邮件(确认,重置密码)包含链接,这些链接需要特定的子域是正确的。
我的环境
rails (4.2.0)
sidekiq (3.3.1)
devise (3.4.1)
devise-async (0.9.0)我的before_action在我的application_controller里
class ApplicationController < ActionController::Base
before_action :set_action_mailer_default_url_options
private
def set_action_mailer_default_url_options
host = "my-logic-to-get-the-correct-host"
ActionMailer::Base.default_url_options = {:host => host}
end
end现在,如果我想重置密码,我总是得到默认的url选项,这是我在环境文件中指定的。如果我将default_url_options从我的环境中删除,就会在我的侧翼日志中得到默认的设计错误。
ActionView::Template::Error: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true主机总是在控制器中正确设置。我已经试过了。主机似乎没有被传递给..。知道我怎么能解决这个问题吗?
我无法将子域保存在某个地方,因为用户可以触发来自不同子域的电子邮件,而不是总是相同的。我需要一种方法来确定应该使用哪个主机发送特定的电子邮件。我可以覆盖设计邮件程序并传递主机或类似的东西吗?
在我的情况下,以下解决方案是不可能的:Dynamic domain name in Rails action mailer while using sidekiq
顺便说一句:设计、设计-异步和sidekiq本身的完整工作流工作(在开发、阶段和生产中)。只有链接的主机是不正确的->,这是我的大问题:-)..
发布于 2016-02-24 08:31:31
我最终得到了以下解决方案。此解决方案包含有多个客户的情况,这些客户可以为邮件中的urls设置自己的电子邮件设置和自己的域/子域。
我使用了自己的mailer类,并加入了客户可能的自定义电子邮件设置:
config/initializers/devise.rb:
config.mailer = "DeviseMailer"config/seculs.yml:
development:
mailer_settings:
customers:
customer_1_identifier_name:
send:
address: "smtp.gmail.com"
port: 587
domain: "example"
user_name: "test@example.com"
email: "noreply@example.com"
password: "secret"
authentication: "plain" # or "login"
ssl: false # or true
tls: false # or true
enable_starttls_auto: true # or falseapp/mailers/devise_mailer.rb:
class DeviseMailer < Devise::Mailer
layout 'mailer'
after_action :set_email_settings_and_sender, only: [:reset_password_instructions, :confirmation_instructions]
def reset_password_instructions(record, token, opts={})
@account = record.current_account if record.class == User # set the account of current_user - current_account is an own helper method
super
end
def confirmation_instructions(record, token, opts={})
@account = record.current_account if record.class == User && record.unconfirmed_email.present?
super
end
private
# re-set the email settings for the given user and his account:
def set_email_settings_and_sender
if @account.present? && @account.custom_email_settings?
mail.reply_to = nil
settings = Rails.application.secrets.mailer_settings["customers"][@account.identifier_name]["send"].symbolize_keys
mail.delivery_method.settings.merge!(settings)
mail.from = "#{@account.display_name} <#{settings[:email]}>"
end
end
end更改电子邮件视图模板中的urls:
app/views/devise/mailer/confirmation_instructions.html.erb
使用新的url选项简单地更改链接..。
app/views/devise/reset_password_instructions.html.erb
<% if @account.present? && @account.domain.present? %>
<% link = "#{@account.host_for_links}/auth/password/edit?reset_password_token=#{@token}" %>
<a href="<%= link %>"><%= link %></a>
<% else %>
<a href="<%= edit_password_url(@resource, reset_password_token: @token) %>"><%= edit_password_url(@resource, reset_password_token: @token) %></a>
<% end %>这是我的解决方案..。如果你有什么问题就告诉我。
编辑:不要忘记在您的环境中设置默认的url选项。例如,在您的development.rb -config/environments/Development.rb中:
config.action_mailer.default_url_options = { host: "your-host.dev", port: 3000 }发布于 2015-02-13 10:02:55
可以在ApplicationController中覆盖默认的url选项,如下所示:
overview.html#default-url-options
class ApplicationController < ActionController::Base
def default_url_options
{:host => host}
end
end或者你可以试试这个答案:https://stackoverflow.com/a/5725343/595152
https://stackoverflow.com/questions/28490680
复制相似问题