2014-03-05 34 views
3

我試圖將我的Rails電子郵件模板移植到使用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寶石的包裝類的功能,而不是指定的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 

但我只是得到一個​​錯誤。

我也用鬍子來指定的郵件模板部分嘗試:

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

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

這只是給了我一個不同的「未找到」的錯誤。

回答

3

下面是使用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 

我認爲你缺少的是同時配置封裝模塊和模板路徑的需要。