views:

71

answers:

1

Hello everyone, 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!

+1  A: 

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 /app/mailer/contact_us_mailer.rb

class ContactUsMailer < ActionMailer::Base

  default :to => "[email protected]"

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

and a /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…

Yannis