I have build a contact form with mail_form gem. I have two different contact persons that need to receive message on two different mails. So i have done contacts and messages controllers and the contact and message models. I can send email with the contacts form, but I cannot do it with the messages form.
I only get this message from the server when I am trying to send email with message form:
Started POST "/messages" for 127.0.0.1 at 2020-07-20 10:11:27 +0200
Processing by MessagesController#create as HTML
Parameters: {"authenticity_token"=>"lQKACWLv72xM6tjq9wvpWNbKZ65QMpdXpnrrGIIemvfFmPfAEOoDtQN/
qUSjU7lZdhTHcqKEPMblhf5zHUtgRg==", "message"=>{"name"=>"me", "email"=>"[email protected]",
"message"=>"Hello", "nickname"=>""}, "commit"=>"Send Message"}
Rendering messages/new.html.erb within layouts/application
Rendered messages/new.html.erb within layouts/application (Duration: 2.7ms | Allocations: 959)
[Webpacker] Everything's up-to-date. Nothing to do
and i don`t know how to check for other error messages than what server display....
So this send emails:
app/controllers/contacts_controller.rb:
class ContactsController < ApplicationController
def new
@contact = Contact.new
end
def create
@contact = Contact.new(params[:contact])
@contact.request = request
if @contact.deliver
flash.now[:error] = nil
redirect_to root_path, notice: 'Message sent successfully'
else
flash.now[:error] = 'Cannot send message'
render :new
end
end
end
app/modles/contact.rb
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 => "[email protected]",
:from => %("#{name}" <#{email}>)
}
end
end
app/views/contacts/new.html.erb
<%= form_for @contact do |f| %>
<% if flash[:error].present? %>
<p> flash[:error]</p>
<% end %>
<%= f.label :name %> <br>
<%= f.text_field :name, required: true %>
<br>
<%= f.label :email %> <br>
<%= f.text_field :email, required: true %>
<br>
<%= f.label :message %> <br>
<%= f.text_area :message, as: :text %>
<%= f.label :nickname %> <br>
<%= f.text_field :nickname, hint: 'leave this field blank' %>
<%= f.submit 'Send Message', class: "button" %>
<% end %>
And the one that dose not send email:
app/controllers/messages_controller.rb:
class MessagesController < ApplicationController
def new
@message = Message.new
end
def create
@message = Message.new(params[:contact])
@message.request = request
if @message.deliver
flash.now[:error] = nil
redirect_to root_path, notice: 'Message sent successfully'
else
flash.now[:error] = 'Cannot send message'
render :new
end
end
end
app/models/message.rb:
class Message < 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 => "[email protected]",
:from => %("#{name}" <#{email}>)
}
end
end
app/views/messages/new.html.erb:
<%= form_for @message do |f| %>
<% if flash[:error].present? %>
<p> flash[:error]</p>
<% end %>
<%= f.label :name %> <br>
<%= f.text_field :name, required: true %>
<br>
<%= f.label :email %> <br>
<%= f.text_field :email, required: true %>
<br>
<%= f.label :message %> <br>
<%= f.text_area :message, as: :text %>
<%= f.label :nickname %> <br>
<%= f.text_field :nickname, hint: 'leave this field blank' %>
<%= f.submit 'Send Message', class: "button" %>
<% end %>
In your MessagesController, you're expecting a param called
contact
Change it like this:
Instead of having
:contact
for the expected param.Should work.