【问题标题】:Rails different Devise Mailer layout for the same actionRails 不同的 Devise Mailer 布局用于相同的操作
【发布时间】:2016-10-13 06:37:59
【问题描述】:

我有一个模型用户,它有多个引用的“配置文件”。一个用户可以有几个这样的配置文件,每个配置文件都应该在电子邮件中引入一个特定的布局。让我们考虑一下配置文件AdminWorker

例如,在我的设计确认控制器中,我需要不同的布局,具体取决于用户拥有的配置文件。例如,如果用户有

  • 管理员配置文件:呈现管理员模板
  • 工作人员配置文件:渲染工作人员模板
  • 两者:渲染另一个模板(混合)

因此,我无法为 Mailer/Controller 设置布局,但我需要在控制器操作中进行设置。假设我有一个助手 layout_for_user() 可以返回给定用户的布局名称。我将如何使用它?例如使用设计邮件?

class MyDeviseMailer < Devise::Mailer
    def confirmation_instructions(record, token, options={})
      # whange layout based on `layout_for_user(record)`
      @token = token
      devise_mail(record, :confirmation_instructions, opts)
    end
end

【问题讨论】:

    标签: layout devise actionmailer ruby-on-rails-5


    【解决方案1】:

    我必须重写 devise_mail 方法

    def confirmation_instructions
      ...
      @layout = find_layout_for_user(user)
      devise_mail(user, :confirmation_instructions, opts)
    end
    
    # Override to include custom layouts
    def devise_mail(record, action, opts={})
      initialize_from_record(record)
      mail(headers_for(action, opts)) do |format|
        format.html { render layout: @layout }
      end
    end
    

    【讨论】:

      猜你喜欢
      • 2011-04-08
      • 2014-01-15
      • 2016-04-11
      • 2021-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多