简体   繁体   English

如何在 rails 邮件程序预览中获取 request.host?

[英]How do I get request.host in rails mailer preview?

Awesome: rails now offers a way to preview mailer views using a class that inherits from很棒:rails 现在提供了一种使用继承自的类来预览邮件程序视图的方法

 ActionMailer::Preview

Rails seems to be using this controller to set all this magic up: Rails 似乎正在使用这个控制器来设置所有这些魔法:

Rails::MailersController#preview

Unfortunately, my app is multi-tenant, and my mailers rely on the request.host being passed to them so that they are styled according to the domain that they were triggered from.不幸的是,我的应用程序是多租户的,我的邮件程序依赖于传递给它们的 request.host,以便根据触发它们的域对它们进行样式设置。

I would love to be able to key off of the url in in the address bar when setting up may mailer previews.我希望能够在设置 May 邮件程序预览时关闭地址栏中的 url。 I'm having to do strange things to get this working (partly because I'm creating feature specs for these too), but mostly because I have to set up my mailers in a context that does not have access to the request object, even though my mailers need it to work properly.我不得不做一些奇怪的事情来让它工作(部分是因为我也在为这些创建功能规范),但主要是因为我必须在无法访问请求对象的上下文中设置我的邮件程序,即使尽管我的邮寄者需要它才能正常工作。

class SiteMailerPreview < ActionMailer::Preview
  def support_email_notification
    support_email = SupportEmail.first
    SiteMailer.support_email_notification(support_email, request).deliver
  end

  private

  def request
    request = Struct.new(:host).new
    request.host = 'local.myhost.net'
    request
  end
end

Currently the host is set statically so I can't really test the mailers against multiple hosts.目前,主机是静态设置的,因此我无法真正针对多个主机测试邮件程序。

request will not be available in models and mailers. request在模型和邮件中不可用。 The only way that can enable you to access request in a model or mailer is to pass the request from controller .可以让你访问的唯一方法requestmodelmailer是传递requestcontroller Check the below example:检查以下示例:

class BooksController < ApplicationController
  def create
    book = Book.create(title: 'xyz', author: current_user)
    BookMailer.new_book_added(book.id, request).deliver
  end
end

and the mailer may look like below:邮件程序可能如下所示:

class BookMailer < ActionMailer::Base
     def new_book_added(book_id, request)
        @book = Book.find book_id
        @url  = books_url(@book, host: request.host_with_port )
        mail(:to => @book.author.email,
             :subject => "Your book is published")
     end
end

and since you mentioned your app is multitenant and assuming you're using apartment gem, You can use Apartment::Tenant.current which will give you the subdomain .并且由于您提到您的应用程序是multitenant并且假设您使用的是apartment gem,您可以使用Apartment::Tenant.current它将为您提供subdomain

In rails 5.2 you have ActiveSupport::CurrentAttributes available, from the docs:在 rails 5.2 你有 ActiveSupport::CurrentAttributes 可用,来自文档:

Abstract super class that provides a thread-isolated attributes singleton, which resets automatically before and after each request.抽象超类,提供线程隔离属性单例,在每次请求前后自动重置。 This allows you to keep all the per-request attributes easily available to the whole system.这使您可以轻松地将所有每个请求的属性提供给整个系统。

I currently use it in my app to store request specific things that I need anywhere (even models, mailers, decorators etc. It has been super useful. Using ActiveSupport::CurrentAttributes if set up correctly would allow to do things like:我目前在我的应用程序中使用它来存储我在任何地方都需要的特定请求(甚至模型、邮件程序、装饰器等。它非常有用。如果设置正确,使用 ActiveSupport::CurrentAttributes 将允许执行以下操作:

Current.request

This is the set up that I have in my app:这是我在我的应用程序中的设置:

In my models在我的模型中

#models/current.rb
class Current < ActiveSupport::CurrentAttributes
  attribute :request_id, :user_agent, :ip_address, :user, :request
end

Controllers concerns:控制器关注:

#controllers/set_current_request_details.rb
module SetCurrentRequestDetails
  extend ActiveSupport::Concern

  included do
    before_action do
      Current.request_id = request.uuid
      Current.user_agent = request.user_agent
      Current.ip_address = request.ip
      Current.request = request
    end
  end
end

Controllers:控制器:

#controllers/application_controller.rb
class ApplicationController < ActionController::Base
  include SetCurrentRequestDetails

...

Source: https://api.rubyonrails.org/v5.2/classes/ActiveSupport/CurrentAttributes.html来源: https : //api.rubyonrails.org/v5.2/classes/ActiveSupport/CurrentAttributes.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM