kxdan
kxdan

Reputation: 187

Rails 4.2.5 Unable to find template

Having an issue with rails mailer, whereby rails cannot find a method within my mailer and claims the template cannot be found within the mailer class.

Therefore tests error:

class ContactMailerTest < ActionMailer::TestCase
  test "should return contact email" do
    mail = ContactMailer.contact_email("[email protected]", "Example",
                                   "SomeExample", @comment = "Hello")
    assert_equal ['[email protected]'], mail.to
    assert_equal ['[email protected]'], mail.from
  end
end

As you can see, this is calling the ContactMailer class, and the method contact_email within it

ContactMailer class:

class ContactMailer < ApplicationMailer
  def contact_email(email, name, subject, comment)
    @email = email
    @name = name
    @subject = subject
    @comment = comment

    mail cc: @email
  end
end

However, the test fails, claiming it cannot find the template, this is also non-reproducible IE i have another project with identical code yet no issue arises, is there a solution for this?

Returned Error:
  1) Error:
ContactMailerTest#test_should_return_contact_email:
ActionView::MissingTemplate: Missing template contact_mailer/contact_email     with "mailer". Searched in:
  * "contact_mailer"

app/mailers/contact_mailer.rb:9:in `contact_email'
test/mailers/contact_mailer_test.rb:8:in `block in <class:ContactMailerTest>'

3 runs, 2 assertions, 0 failures, 1 errors, 0 skips

I am running rubymine on windows, ruby version 2.2.3, rails 4.2.5

Thanks!

Upvotes: 1

Views: 590

Answers (1)

rlarcombe
rlarcombe

Reputation: 2986

The test is finding the method OK. The problem is you are missing a view template which is required to properly render contact_email.

Needs to be defined in app/views/contact_mailer/contact_email.html.erb

or app/views/contact_mailer/contact_email.html.haml if you are using HAML.

Upvotes: 3

Related Questions