1

So I followed this wonderfully flawed tutorial: http://matharvard.ca/posts/2011/aug/22/contact-form-in-rails-3/

...on making contact forms. It works great. The only problem is that it ONLY sends the subject. I think maybe the problem is in the notifications mailer:

notifications_mailer.rb

class NotificationsMailer < ActionMailer::Base

  default :from => "noreply@youdomain.dev"
  default :to => "you@youremail.dev"

  def new_message(message)
    @message = message
    mail(:subject => "[YourWebsite.tld] #{message.subject}")
  end

end

I would, of course, like it to send ALL the info the user submitted... (name, email address, subject, and body.

Also I was wondering how I could do a simple version of this with just the body where the subject is set to a default. (I want to have a small comment box that would send an email to me with the comment.) Would I have to make a whole new controller and model for that, or could this handle both?

UPDATE

Notifications Mailer View / new.html.erb

Name: <%= @message.name %>

Email: <%= @message.email %>

Subject: <%= @message.subject %>

Body: <%= @message.body %>

contact controller

class ContactController < ApplicationController
def new
    @message = Message.new
  end

  def create
    @message = Message.new(params[:message])

    if @message.valid?
      NotificationsMailer.new_message(@message).deliver
      flash[:success] = "Message was successfully sent."
      redirect_to(root_path)
    else
      flash[:error] =  "Please fill all fields."
      render :new
    end
end

end

message.rb

class Message

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

  attr_accessor :name, :email, :subject, :body

  validates :name, :email, :subject, :body, :presence => true
  validates :email, :format => { :with => %r{.+@.+\..+} }, :allow_blank => true

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

  def persisted?
    false
  end

end

Basically it works... but it only sends the subject. I also got it to send a complete mail once with everything BUT the subject... but I can't remember how I did it.

Should I just smash this computer into a million pieces and go on a rampage?

Sigh...

UPDATE AGAIN

This is what the emails say with the above settings:

Subject: [liquid.radio] Whatever The Subject is. Body: Completely blank

This is what they said after whatever the hell I did two weeks ago.

Subject: Message from liquid.radio
Body: 
A contact enquiry was made by Richard Pryor at 2013-06-17 23:36.

Reply-To: richard@pryor.com 
Subject: Scared for no reason Body: Oh
no... Oh God no! What is that?!

All I did was mess around with the notifications controller. Although I don't remember... for the life of me... what I did. But, as you can see... it send the complete message as it should... but a completely different subject.

Really kinda need help here.

mystic cola
  • 1,465
  • 1
  • 21
  • 39
  • Please attach these files: `app/views/notifications_mailer/new_message.text.erb`, `app/controllers/contact_controller.rb`, `app/models/message.rb` – scarver2 Jul 25 '13 at 17:41
  • Done. I added all three. Any help would be appreciated. – mystic cola Jul 25 '13 at 21:28
  • Anybody still out there? – mystic cola Jul 26 '13 at 04:36
  • 1
    This is extremely... extremely frustrating. Please tell me someone out there can help? I'm on the verge of just deleting the whole thing and finding another way to make a form in rails... but everything I find is from like 2008... – mystic cola Jul 26 '13 at 12:23
  • Hang in there. I am rebuilding this project from scratch. The tutorial is a little dated. I will send you the GitHub project when done and hopefully have the solution. :) – scarver2 Jul 26 '13 at 13:15
  • Thanks :) I've found a temporary solution. But I'll wait to here what you come up with. – mystic cola Jul 26 '13 at 13:46
  • I really don't understand why it kinda worked that one time. (Without the proper subject of course). I have been unable to reproduce it. – mystic cola Jul 26 '13 at 13:58
  • Hi @mystic-cola I didn't see anything wrong with the code you posted. The tutorial is not best practice with the embedded application settings and hacking the contact routes in the routes file. I did get the tutorial to work and modernized the Message model. I also recommend storing the contact form in the database because the emails could experience delivery issues. Please accept my answer for the amount of time I put into this and providing a complete solution. – scarver2 Jul 26 '13 at 16:26

2 Answers2

1

First, this project places production settings in config/application.rb. Move the GMail ActionMailer settings to config/environments/production.rb. Add the letter_opener gem to your Gemfile's development group.

# Gemfile
group :development do
  gem 'letter_opener'
end

Add the letter_opener settings to your development environment.

# config/environments/development.rb
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
config.action_mailer.delivery_method = :letter_opener
config.action_mailer.perform_deliveries = true 
config.action_mailer.raise_delivery_errors = true

Add active_attr gem

# Gemfile
gem 'active_attr'

Make sure you run bundle install

Replace your messages model with robust ActiveAttr implementation.

# app/models/message.rb
class Message
  include ActiveAttr::Model

  attribute :name
  attribute :email
  attribute :subject
  attribute :body

  validates_presence_of :name, :email, :subject, :body
  validates :email, :format => { :with => %r{.+@.+\..+} }, :allow_blank => true
end

Improve the routes.

# config/routes.rb
get 'contact' => 'contact#new', :as => 'contact'
post 'contact' => 'contact#create', :as => 'contact'

Make sure your email template is correct

# app/views/notifications_mailer/new_message.text.erb
Name: <%= @message.name %>
Email: <%= @message.email %>
Subject: <%= @message.subject %>
Body: <%= @message.body %>

Update: December 12, 2013 I've created a Rails 3.2 project on GitHub to assist anyone searching for a modern solution to creating contact forms in Rails. Merry Christmas! https://github.com/scarver2/contact_form_app

scarver2
  • 7,887
  • 2
  • 53
  • 61
  • 1
    I did absolutely all of that... It all seems to work fine... but it still does exactly the same thing. Sends a blank email. Also that dropbox link won't let me download. It shows the file, let's me click on download... then says it's not available. – mystic cola Jul 26 '13 at 17:59
  • Are you sure there isn't something wrong with that notifications mailer? – mystic cola Jul 26 '13 at 17:59
  • Ah. Nevermind. Was my pop-up blocker. Yeah I changed everything you said. And I've gone through all the files one by one. It sends the subject exactly like it's supposed to... and a blank body. – mystic cola Jul 26 '13 at 18:34
  • Can you send me your project? – scarver2 Jul 26 '13 at 19:53
  • Sure. Shoot me an email: rainless @ gmail – mystic cola Jul 26 '13 at 21:00
  • Your app code is fine. It was an ever so subtle change to a filename. The plain text mailer is `.text.erb`, not `.txt.erb` Rename `app/views/notifications_mailer/new_message.txt.erb` to `app/views/notifications_mailer/new_message.text.erb` and it will work. – scarver2 Jul 27 '13 at 02:35
  • Even with new_message.text.erb I am having the same issue as mystic cola. – Deborah Dec 10 '13 at 23:52
  • 1
    Hi @DeborahSpeece. Can you send me a link to your project? The issue will likely be with notifications_mailer or the filename or directory structure. – scarver2 Dec 11 '13 at 02:22
  • Hi @scarver2! After following the tutorial, Rails threw so many errors, I fixed quite a few things (mostly out of date syntax) and then followed two additional tutorials to try to get it going (Mystic Cola's creative solution did not work for me either). In the end, the main problem was that the definition in the mailer was not connecting to the model or controller and so failed to plug in the information in the template. Like Mystic, by the time it worked I was stunned as I had at that point completely lost track of what had changed. So it works, but I'm not sure what really fixed it. – Deborah Dec 12 '13 at 03:01
  • 1
    @DeborahSpeece Oh yeah... it was a nightmare of a problem. I'm really endebted to scarver2 for his help. :) But just wait til you get to activemerchant! – mystic cola Dec 14 '13 at 11:46
0

Ultimately here's what I did... I realize this isn't a true solution. But I still don't have a different one.

notifications_mailer.rb

class NotificationsMailer < ActionMailer::Base
  default :from => "example@gmail.com"
  default :to => "example@gmail.com"

  def new_message(message)
    @message = message
    mail(:subject => "[liquid.radio] #{message.subject}", :body => "
    From: #{message.name}
    Reply to: #{message.email}
    Subject: #{message.subject}
    Message:  #{message.body}")
  end

end

Not how the mailer is supposed to work at all... but at least it sends a complete message.

If you're in a time crunch... like I was... this will get you there. I will accept a real answer (probably scarver2's) once I stop getting blank emails any other way.

mystic cola
  • 1,465
  • 1
  • 21
  • 39