将自定义模板添加到mail_form gem

时间:2016-03-16 17:27:01

标签: ruby-on-rails mail-form

我正在尝试按照Mail_form gem的文档添加自定义电子邮件模板。文档说明:

要自定义使用的电子邮件模板,请在contact.erb中创建名为app/views/mail_form的文件。查看此仓库中的lib / mail_form / views / mail_form / contact.erb,了解默认模板的工作原理。

另外,我发现了issue in the repo并做了同样的事情:

我在初始值设定项中创建了一个mail_for.rb文件,并将MailForm::Notifier.template_root = File.join(Rails.root, "app", "views")添加到文件中。

我在视图中创建了一个mail_form目录,并添加了一个contact.erb文件,并将默认模板复制到该文件中。

但是,当我重新启动服务器时,出现以下错误:

  

启动WEBrick   =&GT; Rails 4.2.5应用程序从http://localhost:3000开发开始   =&GT;运行rails server -h以获取更多启动选项   =&GT; Ctrl-C关闭服务器退出/Users/Tony/.rvm/gems/ruby-2.2.1/gems/actionmailer-4.2.5/lib/action_mailer/base.rb:569:in   method_missing': undefined method template_root ='for   MailForm :: Notifier:Class(NoMethodError)来自   /Users/Tony/Desktop/Corner_Stone/vagrant/src/cornerstone/config/initializers/mail_form.rb:1:in   来自<top (required)>' from /Users/Tony/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:268:in加载'   /Users/Tony/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:268:in block in load' from /Users/Tony/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:240:in load_dependency'来自   /Users/Tony/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:268:in load' from /Users/Tony/.rvm/gems/ruby-2.2.1/gems/railties-4.2.5/lib/rails/engine.rb:652:in 阻止在load_config_initializer'中   /Users/Tony/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.5/lib/active_support/notifications.rb:166:in   来自instrument' from /Users/Tony/.rvm/gems/ruby-2.2.1/gems/railties-4.2.5/lib/rails/engine.rb:651:in load_config_initializer'   /Users/Tony/.rvm/gems/ruby-2.2.1/gems/railties-4.2.5/lib/rails/engine.rb:616:in   每个block (2 levels) in <class:Engine>' from /Users/Tony/.rvm/gems/ruby-2.2.1/gems/railties-4.2.5/lib/rails/engine.rb:615:in 来自   /Users/Tony/.rvm/gems/ruby-2.2.1/gems/railties-4.2.5/lib/rails/engine.rb:615:in   来自block in <class:Engine>' from /Users/Tony/.rvm/gems/ruby-2.2.1/gems/railties-4.2.5/lib/rails/initializable.rb:30:in instance_exec'   /Users/Tony/.rvm/gems/ruby-2.2.1/gems/railties-4.2.5/lib/rails/initializable.rb:30:in   来自run_initializers的run' from /Users/Tony/.rvm/gems/ruby-2.2.1/gems/railties-4.2.5/lib/rails/initializable.rb:55:in 阻止   /Users/Tony/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:226:in   block in tsort_each' from /Users/Tony/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:348:in 阻止(2个级别)in each_strongly_connected_component'   /Users/Tony/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:420:in   block (2 levels) in each_strongly_connected_component_from' from /Users/Tony/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:429:in each_strongly_connected_component_from'来自   /Users/Tony/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:419:in   每个block in each_strongly_connected_component_from' from /Users/Tony/.rvm/gems/ruby-2.2.1/gems/railties-4.2.5/lib/rails/initializable.rb:44:in 来自   /Users/Tony/.rvm/gems/ruby-2.2.1/gems/railties-4.2.5/lib/rails/initializable.rb:44:in   tsort_each_child' from /Users/Tony/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:413:in 来电   /Users/Tony/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:413:in   来自的each_strongly_connected_component_from' from /Users/Tony/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:347:in 阻止来自每个_trtrongly_connected_component'   /Users/Tony/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:345:in   each' from /Users/Tony/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:345:in 来电   /Users/Tony/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:345:in   来自each_strongly_connected_component' from /Users/Tony/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:224:in tsort_each'   /Users/Tony/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:203:in   来自tsort_each' from /Users/Tony/.rvm/gems/ruby-2.2.1/gems/railties-4.2.5/lib/rails/initializable.rb:54:in run_initializers'   /Users/Tony/.rvm/gems/ruby-2.2.1/gems/railties-4.2.5/lib/rails/application.rb:352:in   来自initialize!' from /Users/Tony/Desktop/Corner_Stone/vagrant/src/cornerstone/config/environment.rb:5:in '   /Users/Tony/Desktop/Corner_Stone/vagrant/src/cornerstone/config.ru:3:in   require' from /Users/Tony/Desktop/Corner_Stone/vagrant/src/cornerstone/config.ru:3:in 阻止'来自   /Users/Tony/.rvm/gems/ruby-2.2.1/gems/rack-1.6.4/lib/rack/builder.rb:55:in   instance_eval' from /Users/Tony/.rvm/gems/ruby-2.2.1/gems/rack-1.6.4/lib/rack/builder.rb:55:in 初始化'来自   /Users/Tony/Desktop/Corner_Stone/vagrant/src/cornerstone/config.ru:in   来自new' from /Users/Tony/Desktop/Corner_Stone/vagrant/src/cornerstone/config.ru:in '   /Users/Tony/.rvm/gems/ruby-2.2.1/gems/rack-1.6.4/lib/rack/builder.rb:49:in   来自eval' from /Users/Tony/.rvm/gems/ruby-2.2.1/gems/rack-1.6.4/lib/rack/builder.rb:49:in new_from_string'   /Users/Tony/.rvm/gems/ruby-2.2.1/gems/rack-1.6.4/lib/rack/builder.rb:40:in   来自parse_file' from /Users/Tony/.rvm/gems/ruby-2.2.1/gems/rack-1.6.4/lib/rack/server.rb:299:in build_app_and_options_from_config'   /Users/Tony/.rvm/gems/ruby-2.2.1/gems/rack-1.6.4/lib/rack/server.rb:208:in   来自app' from /Users/Tony/.rvm/gems/ruby-2.2.1/gems/railties-4.2.5/lib/rails/commands/server.rb:61:in app'   /Users/Tony/.rvm/gems/ruby-2.2.1/gems/rack-1.6.4/lib/rack/server.rb:336:in   来自wrapped_app' from /Users/Tony/.rvm/gems/ruby-2.2.1/gems/railties-4.2.5/lib/rails/commands/server.rb:139:in log_to_stdout'   /Users/Tony/.rvm/gems/ruby-2.2.1/gems/railties-4.2.5/lib/rails/commands/server.rb:78:in   来自服务器的start' from /Users/Tony/.rvm/gems/ruby-2.2.1/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:80:in 阻止   /Users/Tony/.rvm/gems/ruby-2.2.1/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:75:in   tap' from /Users/Tony/.rvm/gems/ruby-2.2.1/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:75:in 服务器'来自   /Users/Tony/.rvm/gems/ruby-2.2.1/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:39:in   来自run_command!' from /Users/Tony/.rvm/gems/ruby-2.2.1/gems/railties-4.2.5/lib/rails/commands.rb:17:in '   /用户/托尼/桌面/ Corner_Stone /流浪者/ src目录/基石/斌/导轨:9:   来自require' from /Users/Tony/Desktop/Corner_Stone/vagrant/src/cornerstone/bin/rails:9:in '   /Users/Tony/.rvm/gems/ruby-2.2.1/gems/spring-1.6.4/lib/spring/client/rails.rb:28:in   load' from /Users/Tony/.rvm/gems/ruby-2.2.1/gems/spring-1.6.4/lib/spring/client/rails.rb:28:in 来电   /Users/Tony/.rvm/gems/ruby-2.2.1/gems/spring-1.6.4/lib/spring/client/command.rb:7:in   call' from /Users/Tony/.rvm/gems/ruby-2.2.1/gems/spring-1.6.4/lib/spring/client.rb:28:in 从'跑'   /Users/Tony/.rvm/gems/ruby-2.2.1/gems/spring-1.6.4/bin/spring:49:in   来自<top (required)>' from /Users/Tony/.rvm/gems/ruby-2.2.1/gems/spring-1.6.4/lib/spring/binstub.rb:11:in 加载'   /Users/Tony/.rvm/gems/ruby-2.2.1/gems/spring-1.6.4/lib/spring/binstub.rb:11:in   <top (required)>' from /Users/Tony/Desktop/Corner_Stone/vagrant/src/cornerstone/bin/spring:13:in 需要'来自   /用户/托尼/桌面/ Corner_Stone /流浪者/ src目录/基石/斌/春:13:   来自bin / rails的<top (required)>' from bin/rails:3:in load':3:in   `'

当我删除mail_form.rb文件中的代码时,一切正常,但我想根据自己的喜好自定义邮件通知。该错误表示未定义的方法template_root=' for MailForm::Notifier:Class,但我不知道它的含义。我还在学习,所以这个错误有点过头了,任何帮助都将不胜感激!谢谢!

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题。他们的文档缺少有关自定义模板的信息,这令人非常沮丧。它太模糊了。我需要赞扬我的一位同事寻找快速解决方案。他在Ruby中比我更熟练。

以下是他的所作所为:

  1. 删除/删除初始值设定项中的mail_form.rb文件。我的同事认为宝石默认已经这样做了,经过测试这个理论后我猜他是对的。所以你不需要这个文件。
  2. 在项目树中展开External Libraries文件夹,找到mail_form文件夹。我使用的是RubyMine,所以我不知道你的软件是否能显示出来。告诉我,如果它没有,我将更新我的答案并粘贴初始模板代码。
  3. mail_form文件夹中,展开并访问以下文件lib>mail_form>views>mail_form>contact.erb。此.erb文件是gem使用的初始模板。
  4. 将上面访问的文件内容复制/粘贴到先前在contact.erb目录的mail_form目录中创建的views文件中。这将成为您的自定义模板。
  5. 完成此操作后,您现在可以完全控制电子邮件模板,因此请根据自己的喜好修改代码。