Search code examples
ruby-on-railsrubyajaxruby-on-rails-5mailer

NameError (uninitialized constant Welcome4Controller::EmailMe2Mailer): RAILS


I have a simple ruby on rails form that is posted with ajax. However, I keep receiving this error:

NameError (uninitialized constant Welcome4Controller::EmailMe2Mailer):

app/controllers/welcome4_controller.rb:3:in `contact'

This is the form:

<%= form_for :contact, :remote => true, :method => :post, html: {:class => "form-stacked", id: 'contact_form' } do |f| %>

<div class="col-md-6">
    <div class="input-group input-group-lg wow fadeInUp" data-wow-delay='0.8s'>
        <span class="input-group-addon" id="sizing-addon1">
            <i class="fa fa-user" aria-hidden="true"></i>
        </span>
        <input type="text" class="form-control" aria-describedby='sizing-addon1' placeholder="Full Name" name="fullname" id="fullname" required>
    </div>

    <div class="input-group input-group-lg wow fadeInUp" data-wow-delay='1.2s'>
        <span class="input-group-addon" id="sizing-addon1">
            <i class="fa fa-envelope" aria-hidden="true"></i>
        </span>
        <input class="form-control" aria-describedby='sizing-addon1' placeholder="Email Address" required name="email" id="email">
    </div>

    <div class="input-group input-group-lg wow fadeInUp" data-wow-delay='1.6s'>
        <span class="input-group-addon" id="sizing-addon1">
            <i class="fa fa-phone" aria-hidden="true"></i>
        </span>
        <input class="form-control" aria-describedby='sizing-addon1' placeholder="Phone Number (optional)" name="phone" id="phone">
    </div>
</div>

<div class="col-md-6">
    <div class="input-group wow fadeInUp" data-wow-delay='2s'>
        <textarea class="form-control" cols="80" rows="6" name="message" placeholder="Type here..." style="width:100%" id="message" required></textarea>
    </div>
    <button class="btn btn-lg wow fadeInUp" data-wow-delay='2.4s' id="contact_btn"data-loading-text="<i class='fa fa-circle-o-notch fa-spin'></i> Sending" >Send Your Message</button>
</div>

<% end %>

This is the controller:

class Welcome4Controller < ApplicationController
  def contact
    EmailMe2Mailer.confirmation_2(params[:fullname,:email,:phone,:message]).deliver_now

    render json: { success: true }, status: 200
  end

  def index
  end
end

This is the mailer:

class EmailMe2Mailer < ApplicationMailer
    default from: "**@gmail.com"

  def confirmation_2(fullname,email,phone,message)
    @greeting = "Hi"
    @fullname = fullname
    @email = email
    @phone = phone
    @message = message

    mail to: "**@gmail.com", subject: "contact email"
  end
end

This is the ajax call:

$('#contact_form').submit(function(e) {
    e.preventDefault();
  }).validate({ 
    rules: {
      fullname: {
        required: true,

      },
      email: {
        required: true,
        email: true
      },
      message: {
        required: true,
        minlength: 5
      }
    },
    submitHandler: function (form) {

      var btn = $('#contact_btn');
      btn.button('loading');
      setTimeout(function() {
        btn.button('reset');
      }, 8000);

      $.ajax({
        type: 'POST',
        url: '/contact',
    // data: form.serialize(),
    dataType: 'json',
    async: true,
    data: {

      fullname: $('#fullname').val(),
      email: $('#email').val(),
      phone: $('#phone').val(),
      message: $('#message').val()
    },

    success: function (json) {
      $('#contact').hide();
      $('#output').html(
        '<i class="fa fa-check" aria-hidden="true" id="check" style="border-radius:50%;font-size:80px;text-align:center;color:#E81B00"></i><br><p class="lead" style="font-size:40px;">We have received your message!</p>'
        ).show();
    }
  });
     return false; // for demo
 } 
});

It is basically complaining about passing multiple arguments to the mailer here:

EmailMe2Mailer.confirmation_2(params[:fullname,:email,:phone,:message]).deliver_now

This is where EmailMe2Mail is located:

app => mailers => email_me_2_mailer.rb

this is application_mail.rb

class ApplicationMailer < ActionMailer::Base
  default from: '**@gmail.com'
  layout 'mailer'
end

Solution

  • NameError (uninitialized constant Welcome4Controller::EmailMe2Mailer):

    This is because of the file name being wrong. It should be email_me2_mailer.rb whereas you have it like email_me_2_mailer.rb Changing it to email_me2_mailer.rb should fix your problem.

    Normally Rails expects the file names to be in snake_case with respect to the class names being CamelCase. You can use underscore to check the snake_case string for EmailMe2Mailer like below

    "EmailMe2Mailler".underscore
     => "email_me2_mailler"