4

Rails 3.2.13 Contact form only Sending the subject

 3 years ago
source link: https://www.codesd.com/item/rails-3-2-13-contact-form-only-sending-the-subject.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Rails 3.2.13 Contact form only Sending the subject

advertisements

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 => "[email protected]"
  default :to => "[email protected]"

  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: [email protected]
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.


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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK