31

I want to make a contact us form in Rails 3 with the following fields:

  • Name
  • Email
  • Message title
  • Message body

The posted messages are intended to go to my email address so I don't neccessarily must store the messages in the database. Do I have to use ActionMailer, any gem or plugin for it?

JJD
  • 50,076
  • 60
  • 203
  • 339
rodrigoalvesvieira
  • 7,895
  • 15
  • 63
  • 84
  • 1
    You might be interested in reading about Restful Contact forms as well: http://robots.thoughtbot.com/post/159807170/restful-contact-forms – sivabudh Sep 29 '11 at 03:04

4 Answers4

66

This tutorial is an excellent example - and it's Rails 3

Update:

This article is a better example than the one I posted earlier, works flawlessly

Second Update:

I would also recommend merging-in some of the techniques outlined in this railscast on the active_attr gem, where Ryan Bates walks you through the process of setting up a tabless model for a contact page.

Third Update:

I wrote my own test-driven blog post about it

stephenmurdoch
  • 34,024
  • 29
  • 114
  • 189
  • 1
    This is out of date and uses an unnecessary support model. Although still +1 as it improved my understanding :) – Abe Petrillo Jan 19 '12 at 22:00
  • Agreed Abe, I added some new links, etc – stephenmurdoch Jun 03 '12 at 21:02
  • Do you happen to know how to i18n-ize the attr_accessors outlined in the model of the article you linked to? I defined the following in the .yml, but it is disregarded in the according error message. `de.activerecord.attributes.message.subject: Betreff` – user569825 Jun 11 '12 at 08:55
  • 1
    You're referencing ActiveRecord in your translation. I think you need to use ActiveModel. Try `de.activemodel.attributes.message.subject: Betreff`, and make the necessary changes to your locale file too. It might also help to add `extend ActiveModel::Translation` to your model, but I don't know if that is absolutely necessary. See this related [github issue](https://github.com/cgriego/active_attr/issues/80) – stephenmurdoch Jun 11 '12 at 16:46
9

I updated the implementation to be as close as possible to the REST specification.

Basic setup

You can use the mail_form gem. After installing simply create a model named Message similar as it is described in the documentation.

# app/models/message.rb
class Message < MailForm::Base
  attribute :name,          :validate => true
  attribute :email,         :validate => /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i
  attribute :message_title, :validate => true
  attribute :message_body,  :validate => true

  def headers
    {
      :subject => "A message",
      :to => "contact@domain.com",
      :from => %("#{name}" <#{email}>)
    }
  end
end

This will already allow you to test sending emails via the console.

Contact page

In order to create a separate contact page do the following.

# app/controllers/messages_controller.rb
class MessagesController < ApplicationController
  respond_to :html

  def index
  end

  def create
    message = Message.new(params[:contact_form])
    if message.deliver
      redirect_to root_path, :notice => 'Email has been sent.'
    else
      redirect_to root_path, :notice => 'Email could not be sent.'
    end
  end

end

Setup the routing ..

# config/routes.rb
MyApp::Application.routes.draw do
  # Other resources
  resources :messages, only: [:index, :create]
  match "contact" => "messages#index"
end

Prepare a form partial ..

// app/views/pages/_form.html.haml
= simple_form_for :contact_form, url: messages_path, method: :post do |f|
  = f.error_notification

  .form-inputs
    = f.input :name
    = f.input :email, label: 'Email address'
    = f.input :message_title, label: 'Title'
    = f.input :message_body, label: 'Your message', as: :text

  .form-actions
    = f.submit 'Submit'

And render the form in a view ..

// app/views/messages/index.html.haml
#contactform.row
  = render 'form'
JJD
  • 50,076
  • 60
  • 203
  • 339
  • I try this, but, where you put your SMTP configuration. And this contact forms can work in localhost environment? – Stanmx Jul 24 '13 at 06:25
  • @Stanmx The SMTP configuration goes into `config/environment/development.rb` or `../production.rb`. The [documentation describes a setup for GMail](http://guides.rubyonrails.org/action_mailer_basics.html#action-mailer-configuration-for-gmail) which also works when running on localhost. – JJD Jul 24 '13 at 08:47
  • Thank you @JJD, im start with another example, but i every i send, i received: wrong argument (Fixnum)! (Expected kind of OpenSSL::SSL::SSLContext) – Stanmx Jul 24 '13 at 08:55
  • @Stanmx Please open a new question describing your setup. Feel free to link it here. – JJD Jul 24 '13 at 10:22
1

I couldn't make the code of this example work and I think it makes things a bit complex since your creating a model.

Anywat, I made a working contact form and blogged about it.. the text is in portuguese but the code itself is (mostly) in english http://www.rodrigoalvesvieira.com/formulario-contato-rails/

Note: I used sendmail, not SMTP.

Arslan Ali
  • 17,418
  • 8
  • 58
  • 76
rodrigoalvesvieira
  • 7,895
  • 15
  • 63
  • 84
-1

You can use Contact Us gem via this link: https://github.com/JDutil/contact_us The documentation is clear and you can use it simply.

Features:

  1. Validation
  2. Easy/Add remove fields
  3. Simple configuration
Hieu Pham
  • 6,577
  • 2
  • 30
  • 50
Aboozar Rajabi
  • 1,683
  • 21
  • 26