Question

First off, i know attr_accessible is deprecated in Rails 4, but what about attr_accessor?

When you hit submit it's returning a "Template is Missing" error, but that's because its hitting an error somewhere in the send proccess then just trying to return "connect#create" which doesn't exist as a physical page.

When i check the log files, I am getting a 500 internal server error when trying to send a contact form, and I'm not sure if using attr_accessor in Rails 4 is the culprit. Is there a newer way to write this?

class Message

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

  attr_accessor :name, :email, :phone, :subject, :company, :title, :market, :body

  validates :name, :email, :subject, :company, :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

The above is the message model for the contact form:

Is it something within the process of sending the data?

The rest of the code for the contact mail functionality is:

Contact Mailer

class ContactMailer< ActionMailer::Base

  default :from => "noreply@test.com"
  default :to => "{{MY EMAIL}}"

  def new_message(message)
    @message = message
    mail(:subject => "Test Message")
  end

end

In /views/contact_mailer/ there is a new_message.text.erb file:

Name: <%= @message.name %>

Email: <%= @message.email %>

Phone: <%= @message.phone %>

Subject: <%= @message.subject %>

Company: <%= @message.company %>

Title: <%= @message.title %>

Market: <%= @message.market %>

Body: <%= @message.body %>

My Routes are:

match 'connect' => 'connect#index', :as => 'connect', :via => :get
match 'connect' => 'connect#create', :as => 'connectpost', :via => :post

The connect page controller:

class ConnectController < ApplicationController

  def index
    @message = Message.new
  end

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

    if @message.valid?
      NotificationsMailer.new_message(@message).deliver
      redirect_to(connect_path, :notice => "Message was successfully sent.")
    else
      flash.now.alert = "Please fill all fields."
      render :new
    end
  end

end

And finally....the SMTP settings in /config/initializers/smtp_settings.rb

ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "{{SITE DOMAIN}}",
:user_name => "{{GMAIL EMAIL}}",
:password => "{{GMAIL EMAIL PASSWORD}}",
:authentication => "plain",
:enable_starttls_auto => true
}
Was it helpful?

Solution

My ConnectController#Create was trying to initialize

NotificationMailers.new_message()

But it needed to be:

ContactMailer.new_message()

I have no idea why the tutorial I followed would have the wrong class name there...but that was the issue.

Thanks all.

OTHER TIPS

attr_accessor, attr_writer and attr_reader are all part of vanilla core Ruby and are helper methods for Modules and Classes.

They work fine in Ruby 2.0, so you'll have to mark them off your suspect list.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top