【问题标题】:Rendering mailer templates with Mustache in Rails在 Rails 中使用 Mustache 渲染邮件模板
【发布时间】:2014-03-05 11:10:36
【问题描述】:

我正在尝试使用 stache gem 将我的 Rails 电子邮件模板移植到 mustache。

我有一个标准的邮件操作,它只是向用户发送电子邮件。我已重命名模板路径,以防止在为与此邮件操作关联的模板创建存储视图类时发生命名冲突。 the Rails guides 中概述了此过程。

# app/mailers/registration_mailer.rb
class RegistrationMailer < ActionMailer::Base
  def bailed(user)
    @user = user
    mail to: user.email, template_path: 'er_registration_mailer'
  end
end

这是与上述操作关联的 Stache 视图。请注意,模块名称与上面的模板路径匹配。

# app/views/er_registration_mailer/bailed.rb
module ErRegistrationMailer
  class Bailed < ::Stache::Mustache::View
    def continue_registration_link
      link_to "Continue registration by connecting your Twitter account", 
        connect_registrations_url
    end

    def signature
      @view.render "shared/mailer/sig"
    end
  end
end

我终于有了一个用于我的邮件的小胡子模板。

# app/templates/er_registration_mailer/bailed.html.mustache
<p>Hi there!</p>

<p>I noticed you recently started the signup process for my app but didn't complete it.</p>

<p>{{{continue_registration_link}}}</p>

{{{signature}}}

当我尝试发送电子邮件时,我在尝试呈现 signature 部分时收到错误消息。这部分生活在app/templates/shared/mailer 中,我有胡子和erb 版本,分别称为_sig.html.mustache_sig.html.erb

这是错误:

Failure/Error: RegistrationMailer.bailed(user).deliver
  ActionView::Template::Error:
    Missing partial shared/mailer/sig with {:locale=>[:en], :formats=>[:html, :text, :js, :css, :ics, :csv, :png, :jpeg, :gif, :bmp, :tiff, :mpeg, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json, :pdf, :zip], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee, :slim, :arb, :rb, :mustache]}. Searched in:
      * "/Users/davidtuite/dev/shareshaper/app/app/views"
      * "/Users/davidtuite/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/bundler/gems/active_admin-6f04ed5cec24/app/views"
      * "/Users/davidtuite/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/devise-3.2.2/app/views"
      * "/Users/davidtuite/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/foundation-rails-5.0.3.1/app/views"
      * "/Users/davidtuite/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/kaminari-0.15.1/app/views"
  # ./app/views/er_registration_mailer/bailed.rb:17:in `signature'

Rails 似乎正在 app/views 中搜索模板(或视图类,我不确定是哪个)。

如何让局部正确渲染?

我正在使用 Rails 4.0.3、Ruby 2.1.1 和 stache 1.0.3。

我尝试过的事情

使用 stache gem 的包装类功能,而不是为邮件指定 template_path 并为视图类的名称间距添加前缀。

我都试过了:

# app/views/registration_mailer/bailed.rb
module Wrapper
  module RegistrationMailer
    class Bailed < ::Stache::Mustache::View
    end
  end
end

和(注意目录结构):

# app/views/wrapper/registration_mailer/bailed.rb
module Wrapper
  module RegistrationMailer
    class Bailed < ::Stache::Mustache::View
    end
  end
end

但我只是收到Uninitialized const: Wrapper 错误。

我也尝试过使用 mustache 在邮件模板中指定部分:

# app/templates/er_registration_mailer/bailed.html.mustache
<!-- HTML as in above code sample -->

{{{>shared/mailer/sig}}}

这只是给了我一个不同的“未找到”错误。

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-4 actionmailer mustache


    【解决方案1】:

    下面是一个将 Stache 与邮件程序一起使用的示例:

    # config / initializers / stache.rb
    Stache.configure do |c|
      c.template_base_path = Rails.root.join('app', 'views')
      c.wrapper_module_name = "Wrapper"
    
      c.use :mustache
    end
    
    # app / mailers / registration_mailer.rb
    class RegistrationMailer < ActionMailer::Base
      default template_path: 'mailers/registration_mailer'
    
      def bailed(user)
       @user = user
       mail to: user.email
     end
    end
    
    # app / models / wrapper / mailers / regisration_mailer / bailed.rb
    module Wrapper
      module Mailers
        module RegistrationMailer
          class Bailed < ::Stache::Mustache::View
            def continue_registration_link
              link_to "Continue registration by connecting your Twitter account", 
                connect_registrations_url
            end
    
            def signature
              @view.render "shared/mailer/sig"
            end
          end
        end
      end
    end
    

    我认为您缺少的是需要配置包装器模块和模板路径。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-25
      • 1970-01-01
      • 2011-11-26
      • 2015-03-11
      • 1970-01-01
      • 1970-01-01
      • 2012-02-11
      相关资源
      最近更新 更多