11

I simply want a contact us form with name, email and message fields in my Rails app, I don't want to save(permanently) the message I just want to send the message as an email for a email account of mine. Can you help me?

Thanks!

rodrigoalvesvieira
  • 7,895
  • 15
  • 63
  • 84

3 Answers3

15

In Rails3, you can create an ActiveModel model:

# /app/models/contact_us.rb
class ContactUs

  include ActiveModel::Validations
  include ActiveModel::Conversion
  extend ActiveModel::Naming

  attr_accessor :name, :email, :message

  def initialize(attributes = {})
    attributes.each do |name, value|
      send("#{name}=", value)
    end
  end

  def persisted?
    false
  end
end

then a mailer:

# /app/mailer/contact_us_mailer.rb
class ContactUsMailer < ActionMailer::Base

  default :to => "your@address.com"

  def send(message)
    @message = message
    mail( :subject => @message.subject, :from => @message.email ) do |format|
      format.text
    end
  end
end

and a view:

# /app/views/contact_us_mailer/sent.text.erb
Message sent by <%= @message.name %>
<%= @message.message %>

I didn't test this code exactly, but I just want to let you get the idea…

JJD
  • 50,076
  • 60
  • 203
  • 339
Yannis
  • 5,426
  • 1
  • 31
  • 30
  • 3
    Why is this the best way? Why create a model for it in the first place? Why not just have the contact_us form post to a contact_us action on the controller that then triggers the mailer? – AdamT Aug 31 '12 at 04:16
  • This is a good example, just don't use "send" as a method name as it's a reserved keyword. The reason for having a model is to add validations, so you'd want to check the model with valid? in your controller. – John Glass Feb 17 '14 at 01:37
7

I've written a Rails Engine https://github.com/jdutil/contact_us that you can easily drop into any Rails 3+ application. I didn't add a Name field to the form, but you could fork the repo then modify it to suit your needs. It does require the Formtastic gem since I wanted an easy way to hook into peoples existing form styles though.

To install the Engine add the contact_us gem to your Gemfile:

gem 'contact_us', '~> 0.4.0'

Run bundle and the install rake task:

$ bundle
$ bundle exec rake contact_us:install

Then just modify the generated initializer in /config/initializers/contact_us.rb to have the email you want the form submissions sent to.

JDutil
  • 3,622
  • 25
  • 32
  • @jDutil do we need to add SMTP settings in the evironment config files? – mrudult Oct 05 '13 at 05:06
  • You will need to setup your ActionMailer to deliver your emails however you would like the emails to be sent. The contact_us gem simply provides the form and mailer it's up to you to configure your applications method of sending mail. – JDutil Oct 26 '13 at 06:25
3

I made a working 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/

Thanks!

rodrigoalvesvieira
  • 7,895
  • 15
  • 63
  • 84
  • 1
    Broken link. Also redirects to a page shows a drawing of a phallus which is not the definition of SFW (Safe For Work) which is a place where someone would most probably search for a rails contact form. Obrigado. – Dimitrios Mistriotis Dec 01 '17 at 12:01