0
Modelo contact.Rb:
class Contact
include ActiveModel::Validations
include ActiveModel::Conversion
extend ActiveModel::Naming
attr_accessor :name, :email, :message, :subject
validates_presence_of :name, :subject, :email, :message
validates_format_of :email, with: /\A[a-z0-9.]+(\+[a-z0-9_-]+)?@[a-z0-9.-]+\.[a-z]{2,4}\z/
validates_length_of :message, minimum: 10, maximum: 500
def initialize(attributes = {})
attributes.each do |name, value|
send("#{name}=", value)
end
end
def persisted?
false
end
end
Controller:
def sendemail
@contact = Contact.new(set_params)
if @contact.valid?
PageContact.contact_message(@contact).deliver
flash.notice = "E-mail enviado com sucesso!"
else
flash.alert = "* Preencha os campos corretamente."
end
redirect_to root_path
end
private
def set_params
params.permit(:name, :email, :subject, :message)
end
config / Environments / Development.Rb
config.action_mailer.delivery_method = :smtp
#config.action_mailer.smtp_settings = { :address => "localhost", :port => 1025 }
config.action_mailer.smtp_settings = {
:address => "smtp.mandrillapp.com",
:port => 587,
:domain => 'http://localhost:3000',
:user_name => ENV['EMAIL_USER'],
:password => ENV['EMAIL_PWD'],
:authentication => 'login',
:enable_starttls_auto => true
}
app / views / home / index.html.haml
%div{:class=>"8u 12u$(small)"}
= form_tag root_path do
%div{:class=>"row uniform 50%"}
%div{:class=>"12u 12u$(xsmall)"}
= text_field_tag :name, nil, placeholder: "Nome", required: true
%div{:class=>"12u 12u$(xsmall)"}
= text_field_tag :subject, nil, placeholder: "Assunto", required: true
%div{:class=>"12u$"}
= email_field_tag :email, nil, placeholder: "E-mail", required: true
%div{:class=>"12u$"}
= text_area_tag :message, nil, rows: 4, placeholder: "Mensagem", required: true
%br
%ul.actions
%li
= submit_tag "Enviar"
The problem I realized is that you’re not going through validation.
On the Rails console (
rails c
) it is possible to create an object Contact with the same parameters that are sent in the form? i.e. calling@contact.save
is returnedtrue
? Otherwise, also check the console for callback@contact.errors
.– Bruno Coimbra
Display this message: Nametown: Undefined method `save' for #<Contact:0x007fa0031e2490>
– Thiago Porto
But Valid returns true.
– Thiago Porto