1

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

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

另外,我在 repo 中发现了这个问题并做了同样的事情:

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

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

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

启动 WEBrick => Rails 4.2.5 应用程序开始在http://localhost:3000上开发 => 运行rails server -h以获得更多启动选项 => 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 methodtemplate_root=' for MailForm::Notifier:Class (NoMethodError) from /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:inload' 来自 /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:inload_dependency ' 来自 /Users/Tony/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:268:inload' from /Users/Tony/.rvm/gems/ruby-2.2.1/gems/railties-4.2.5/lib/rails/engine.rb:652:in 在 /Users/Tony/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.5/lib/active_support/notifications.rb:166:in load_config_initializer' 中阻止来自 /Users/Tony/.rvm 的 instrument' from /Users/Tony/.rvm/gems/ruby-2.2.1/gems/railties-4.2.5/lib/rails/engine.rb:651:in load_config_initializer' /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 each' 来自 /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' from /Users/Tony/.rvm/gems/ruby-2.2.1/gems/railties-4.2.5/lib/rails/initializable.rb:55:in block in run_initializers' from /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 block (2 levels) in each_strongly_connected_component' from /用户/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 each' 来自 /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 call' 来自 /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 block in each_strongly_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 call' 来自 /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 initialize' 来自 /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 block in server' 来自 /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 server' 来自 /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 '来自 /Users/Tony/Desktop/Corner_Stone/vagrant/src/cornerstone/bin/rails:9:in 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 call' 来自 /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 run' 来自 /Users/Tony/.rvm/gems/ruby-2.2.1/gems/spring-1.6。 4/bin/弹簧:49:在 <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 来自 /Users/Tony/Desktop/Corner_Stone/ vagrant/src/cornerstone/bin/spring:13:in <top (required)>' from bin/rails:3:inload' 从 bin/rails:3:in `'

当我删除文件中的代码时,一切正常mail_form.rb,但我想根据自己的喜好自定义邮件通知。错误说 undefined method template_root=' for MailForm::Notifier:Class,但我不知道它是什么意思。我仍在学习,所以这个错误有点过头了,任何帮助将不胜感激!谢谢!

4

1 回答 1

1

我面临着同样的问题。令人沮丧的是,他们的文档缺少有关自定义模板的信息。太模糊了 我需要感谢我的一位同事找到了一个快速的解决方案。他在Ruby方面比我熟练得多。

这是他所做的:

  1. 删除/删除mail_form.rb初始化程序中的文件。我的同事认为 gem 已经默认这样做了,在测试了这个理论之后,我猜他是对的。所以你不需要这个文件。
  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. 完成此操作后,您现在可以完全控制电子邮件模板,因此可以根据自己的喜好修改代码。
于 2016-03-18T16:01:20.527 回答