在Rails中使用八字胡呈现邮件模板

时间:2021-07-03 18:08:50

I'm trying to port my Rails email templates over to mustache using the stache gem.

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

I have a standard mailer action which simply sends an email to a user. I've renamed my template path in order to prevent a naming * when I create the stash view class for the template associated with this mailer action. This process is outlined in the Rails guides.

我有一个标准的邮件发送操作,它只向用户发送电子邮件。我已经重命名了模板路径,以便在为与此mailer操作关联的模板创建隐藏视图类时避免命名冲突。这个过程在Rails指南中进行了概述。

# 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

This is the Stache view associated with the action above. Notice that the module name matches the template path above.

这是与上面的操作相关联的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

Finally I have a mustache template for my mailer.

最后,我为我的邮递员准备了一个胡子模板。

# 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}}}

When I try to send an email, I get an error when it tries to render the signature partial. This partial lives in app/templates/shared/mailer where I have both mustache and erb versions of it called _sig.html.mustache and _sig.html.erb respectively.

当我试图发送电子邮件时,当它试图呈现签名部分时,我将得到一个错误。这个部分存在于app/模板/共享/邮件中,我在其中有mustache和erb版本的_sig.html。胡子和_sig.html。分别erb。

Here's the error:

这是一个错误:

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 appears to be searching in app/views for the template (or view class, I'm not sure which).

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

How can I get the partial to render correctly?

怎样才能使局部渲染正确?

I'm using Rails 4.0.3, Ruby 2.1.1 and stache 1.0.3.

我使用的是Rails 4.0.3、Ruby 2.1.1和stache 1.0.3。

Things I've tried

我已经试过

Using the wrapper class functionality of the stache gem instead of specifying the template_path to mailers and prefixing the name spacing of the view class.

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

I've tried both:

我试过两个:

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

and (note the directory structure):

(注意目录结构):

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

But I just get an Uninitialized const: Wrapper error.

但是我得到了一个未初始化的const:包装器错误。

I've also tried using mustache to specify the partial in the mailer template:

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

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

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

That just gives me a different 'not found' error.

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

1 个解决方案

#1


3  

Here is an example of using Stache with a mailer:

这里有一个使用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

I think what you are missing is the need to configure both the wrapper module and the template path.

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

#1


3  

Here is an example of using Stache with a mailer:

这里有一个使用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

I think what you are missing is the need to configure both the wrapper module and the template path.

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