title: ActionMailer and Validations
body: <p>I have a rails actionMailer contact form which send emails but doesn't check if it dosen't  check the form for errors.
  If I send an incorrect email: <code>ewfw</code> as opposed to a correct email: <code>test@test.com</code>
  they both send, if the form is blank no email will get sent, and no error message comes up as well, but the notification alart works if the email get sent. </p>
  
  <p>Any help would be appreciated.  </p>
  
  <p><em>models/support.rb</em></p>
  
  <pre><code>class Support
    include ActiveModel::Validations
  
    validates_presence_of :email, :sender_name, :support_type, :content
    # to deal with form, you must have an id attribute
    attr_accessor :id, :email, :sender_name, :support_type, :content
  
    def initialize(attributes = {})
      attributes.each do |key, value|
        self.send("#{key}=", value)
      end
      @attributes = attributes
    end
  
    def read_attribute_for_validation(key)
      @attributes[key]
    end
  
    def to_key
    end
  
    def save
      if self.valid?
        Notifier.support_notification(self).deliver!
        return true
      end
      return false
    end
  end
  </code></pre>
  
  <p>*controllers/supports_controller.rb*</p>
  
  <pre><code>class SupportsController &lt; ApplicationController
    def new
      # id is required to deal with form
      @support = Support.new(:id =&gt; 1)
  
  
  
    end
  
    def create
      @support = Support.new(params[:support])
      if @support.save
        redirect_to('/contact', :notice =&gt; "Your message was successfully sent.")
      else
        flash[:alert] = "You must fill all fields."
        render 'new'
      end
    end
  end
  </code></pre>
  
  <p>*views/support/form_.html.erb*</p>
  
  <pre><code>&lt;% form_for @support, :url =&gt; { :action =&gt; "create" }, :html =&gt; { :method =&gt; :post } do |f| %&gt;
  
  
    &lt;p&gt;
      &lt;%= f.label "Name" %&gt;
    &lt;/p&gt;
    &lt;p&gt;
      &lt;%= f.text_field :sender_name, "size" =&gt; 37 %&gt;
    &lt;/p&gt;
    &lt;p&gt;
      &lt;%= f.label "Email" %&gt;
    &lt;/p&gt;
    &lt;p&gt;
      &lt;%= f.text_field :email, "size" =&gt; 37 %&gt;&lt;br /&gt;&lt;br /&gt;
    &lt;/p&gt;
    &lt;p&gt;
      &lt;%= f.label "Subject" %&gt;
    &lt;/p&gt;
    &lt;p&gt;
      &lt;%= f.select :support_type, options_for_select(["Hire", "General", "Collaboration"]) %&gt;
    &lt;/p&gt;
    &lt;p&gt;
      &lt;%= f.label "Details" %&gt;
    &lt;/p&gt;
    &lt;p&gt;
      &lt;%= f.text_area :content, "rows" =&gt; 3, "cols" =&gt; 27  %&gt;
    &lt;/p&gt;
    &lt;p&gt;&lt;br /&gt;
      &lt;%= f.submit "Submit" %&gt;
    &lt;/p&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p><em>Initializers/mailer.rb</em></p>
  
  <pre><code># config/initializers/mailer.rb
  ActionMailer::Base.delivery_method = :sendmail
  ActionMailer::Base.perform_deliveries = true #default value
  ActionMailer::Base.raise_delivery_errors = true
  
  ActionMailer::Base.sendmail_settings = {
  
  :tls =&gt; true,
  :address =&gt; 'smtp.test.com',
  :port =&gt; 587,
  :domain =&gt; 'test.com',
  :user_name =&gt; 'test@test.com',
  :password =&gt; '#',
  :authentication =&gt; 'login',
  :openssl_verify_mode=&gt;nil,
  :enable_starttls_auto =&gt; true
  
  
  }
  </code></pre>