I am relatively new in Ruby on Rails programming, and I got this error when trying to use the mail_form gem. The error is in the create function as can be seen:
I think I got all setup the correct way:
My model is like this:
class Contact < MailForm::Base
attribute :name, :validate => :true
attribute :email, :validate => /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i
attribute :message, :validate => :true
attribute :nickname, :captcha => :true
def headers
{
:subject => "Contact Form",
:to => "(my email)",
:from => %("#{name}" <#{email}>)
}
end
end
I don't know if matters that I'm trying to create a site with only one page, so I have the form to create a new contact in the welcome.html.erb of the pages controller, but I am defining the contact:
def welcome
@admin = Admin.find(1)
@projects = Project.all.order('created_at DESC')
@contact = Contact.new
end
Also I think that matters, that the create function (that is throwing the error) is in the contacts controller, I don't know if there was ok or it will be better at the same controller as the Contact.new (in the welcome function of pages):
def create
@contact = Contact.new(params[:contact])
@contact.request = request
if @contact.deliver
redirect_to(root_path, :notice => "Thank you for contacting me. I will reply shortly!")
else
flash.now[:error] = 'Cannot send message.'
end
end
I have tested in production and in development and in the both happen the same error. I am following this tutorial of Mackenzie Child and this didn't happen to him, and we have the same code(except for the fact that I am trying to make this in only one view). I don't know if it helps, but this is what I have in my config/environments/development.rb:
config.action_mailer.delivery_method = :letter_opener
So in development I'm using the gem letter_opener for displaying the messages instead of sending mails, although this affects nothing in the error because the error was showing up way before I installed the gem.
And in production I use sendgrid addon from heroku:
config.action_mailer.default_url_options = { host: 'http://www.luisbarajasp.com' }
config.action_mailer.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:address => 'smtp.sendgrid.net',
:port => '587',
:authentication => :plain,
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:domain => 'heroku.com',
:enable_starttls_auto => true
}
I have searched in many places and in all the questions on stack overflow but I found nothing. If you can help me that would be greate.
Thanks.