我正在尝试使用 stache gem 将我的 Rails 电子邮件模板移植到 mustache .
我有一个标准的邮件程序操作,它只是向用户发送一封电子邮件。我重命名了我的模板路径,以防止在为与此邮件操作关联的模板创建隐藏 View 类时发生命名冲突。 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 View 。请注意,模块名称与上面的模板路径匹配。
# 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
我终于有了一个用于我的邮件程序的 mustache 模板。
# 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
中,我有它的 mustache 和 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
中搜索模板(或 View 类,我不确定是哪个)。
我怎样才能让局部正确渲染?
我正在使用 Rails 4.0.3、Ruby 2.1.1 和 stache 1.0.3。
我尝试过的事情
使用 stache gem 的包装类功能,而不是为邮件程序指定 template_path
并在 View 类的名称间距前加上前缀。
我都试过了:
# 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}}}
这只会给我一个不同的“未找到”错误。
最佳答案
这是一个将 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
我认为您缺少的是需要同时配置包装器模块和模板路径。
关于ruby-on-rails - 在 Rails 中使用 Mustache 渲染邮件模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22196034/