我正在尝试使用stache gem将我的Rails电子邮件模板移植到小胡子上。
我有一个标准的邮件程序操作,它只是向用户发送一封电子邮件。我已经重命名了我的模板路径,以便在为与此邮件程序操作关联的模板创建存储视图类时防止命名冲突。此过程在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
错误。
我也尝试使用小胡子在邮件程序模板中指定部分:
# app/templates/er_registration_mailer/bailed.html.mustache
<!-- HTML as in above code sample -->
{{{>shared/mailer/sig}}}
这只是给了我一个不同的'未找到'错误。
答案 0 :(得分: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
我认为您缺少的是需要配置包装器模块和模板路径。