1
votes

I'm attempting to follow the documentation for the Mail_form gem to add a custom email template. The documentation states:

To customize the e-mail template that is used create a file called contact.erb in app/views/mail_form. Take a look at lib/mail_form/views/mail_form/contact.erb in this repo to see how the default template works.

Also, I found this issue in the repo and did the same:

I created a mail_for.rb file in initializers and added MailForm::Notifier.template_root = File.join(Rails.root, "app", "views"), to the file.

I created a mail_form directory in views and added a contact.erb file and copied the default template to it.

However, when I restart the server I get the following error:

Booting WEBrick => Rails 4.2.5 application starting in development on http://localhost:3000 => Run rails server -h for more startup options => Ctrl-C to shutdown server Exiting /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' from /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' from /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 block in load_config_initializer' from /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' from /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 each' from /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' from /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 /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' 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' from /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' from /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' from /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' from /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' from /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' from /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 ' from /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 block in ' from /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' from /Users/Tony/Desktop/Corner_Stone/vagrant/src/cornerstone/config.ru:in new' from /Users/Tony/Desktop/Corner_Stone/vagrant/src/cornerstone/config.ru:in ' from /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' from /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' from /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' from /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' from /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' from /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' from /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 ' from /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 ' from /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' from /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' from /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 load' from /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 require' from /Users/Tony/Desktop/Corner_Stone/vagrant/src/cornerstone/bin/spring:13:in <top (required)>' from bin/rails:3:inload' from bin/rails:3:in `'

Everything works when I remove the code in the mail_form.rb file, but I want to customize the mail notification to my liking. The error says undefined method template_root=' for MailForm::Notifier:Class, but I don't know what it means. I'm still learning so this error is a little over my head, Any help would be greatly appreciated! Thanks!

1

1 Answers

1
votes

I was faced with the same problem. It was quite frustrating that their documentation is lacking information about custom template. It's so nebulous. I need to give credit to a colleague of mine for finding a quick solution. He is way more skilled than me in Ruby.

Here's what he did:

  1. Remove/delete the mail_form.rb file in initializers. My colleague thinks the gem already does this by default, and after testing this theory I guess he was right. So you don't need the file.
  2. Expend your External Libraries folder in your project tree and find the mail_form folder. I'm using RubyMine so I don't know if your software will show this. tell me if if it doesn't and I will update my answer and paste the initial template code.
  3. Inside the mail_form folder, expand and access the following file lib>mail_form>views>mail_form>contact.erb. This .erb file is the initial template used by the gem.
  4. Copy/paste the content of the file accessed above into the contact.erb file that you previously created inside your mail_form directory in your views directory. This will become your custom template.
  5. Once this is done, you now have full control over the email template, so modify the code to your liking.