ruby-on-railsmodel-view-controllersmtpgmailmail-form

undefined method `deliver' for #<Contact:0x007fc2fe6a01f8> with gem 'mail_form'


Im trying to download this gem https://github.com/heartcombo/mail_form and I am getting this error undefined method `deliver' for # In the gem file

   gem 'mail_form'
    gem 'rails', '4.2.11.1'

In production.rb file

config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
    address:              'smtp.gmail.com',
    port:                 587,
    domain:               'gmail.com',
    user_name:            'gmail email',
    password:             'gmail password',
    authentication:       'plain',
    enable_starttls_auto: true  }

In contact controller

class ContactsController < ApplicationController require 'mail_form' def new @contact=Contact.new end

  def create
    @contact = Contact.new(contact_attributes)
      if @contact.deliver
      redirect_to new_contact_path, notice: "Thank you... Your Message was sent successfully."
      else
        flash.now[:error] = "Please correct the form"
        redirect_to new_contact_path
      end
  end

  def index
    @contact=Contact.all
  end

  private

  def contact_attributes
    contact_attributes = params.require(:contact).permit([:name,:email,:message,:phone])
  end
end

In new_contact_path

 <%= form_for @contact do |f| %>
                        <div class="col-sm-5 col-sm-offset-1">
                            <div class="form-group">
                                <label>Name *</label>
                                <%= f.text_field :name, class: "form-control" ,required: "required" %>
                            </div>
                            <div class="form-group">
                                <label>Email *</label>
                                <%= f.text_field :email, class: "form-control", required: "required" %>
                            </div>
                            <div class="form-group">
                                <label>Phone</label>
                                <%= f.text_field :phone, class: "form-control" %>
                            </div>                      
                        </div>
                        <div class="col-sm-5">
                            <div class="form-group">
                                <label>Message *</label>
                                <%= f.text_area :message , id: "message",  required: "required", class: "form-control" ,rows: "8" %></textarea>
                            </div>                      
                            <div class="form-group">
                                <%= f.submit "Submit Message"  %></button>
                            </div>
                        </div>
                        <% end %>

In contact model file

      def headers
        {
          :subject => "My Contact Form",
          :to => "nourfiverr@gmail.com",
          :from => %("#{name}" <#{email}>)
        }
      end

Solution

  • In order to use mail_form gem you need to create another model like in the description or you need to modify your existing model. You can't use these lines directly

    contact_attributes = params.require(:contact).permit([:name,:email,:message,:phone])
    @contact = Contact.new(contact_attributes)
    @contact.deliver
    

    deliver here means nothing for model like this class Contact < ApplicationRecord until you make your contact model like this

    class Contact < ActiveRecord::Base
      include MailForm::Delivery
    
      attributes :name, :email, :message, :form
    
      def headers
        {
          :subject => "My Contact Form",
          :to => "nourfiverr@gmail.com",
          :from => %("#{name}" <#{email}>)
        }
      end
    end
    

    if you don't want to make this modifications just follow steps in gems readme file.