I built a contact form on my rails app using Sendgrid. When filling up the contact form, this error message shows up:
ActionController::ParameterMissing in MessagesController#create
param is missing or the value is empty: message
Started POST "/messages" for ::1 at 2020-12-30 16:41:11 +0100
Processing by MessagesController#create as HTML
Parameters: {"utf8"=>"?", "authenticity_token"=>"uCP2fAxAwk/i8678+LkKyg8LVDpRhrb8CY3sng1z+G7fDMsBzm07IGlG/5Kcqm/GZyzygu0+LKvvUKyVtNuEuA==", "/messages"=>{"name"=>"Coline", "email"=>"test@yopmail.com", "phone_number"=>"0606060606", "message"=>"jhsjhfjqshf"}, "commit"=>"Envoyer"}
Completed 400 Bad Request in 4ms (ActiveRecord: 0.0ms)
Can anyone please help me figure out why?
My message model:
class Message
include ActiveModel::Model
attr_accessor :name, :email, :phone_number, :body
validates :name, :email, :phone_number, :body, presence: true
end
My message controller:
class MessagesController < ApplicationController
def new
@message = Message.new
end
def create
@message = Message.new message_params
if @message.valid?
MessageMailer.contact(@message).deliver_now
flash[:success] = "Votre message a bien été envoyé !"
redirect_to root_path
else
flash[:danger] = "Votre message n'a pas pu être envoyé."
redirect_to root_path
end
end
private
def message_params
params.require(:message).permit(:name, :email, :phone_number, :body)
end
end
My message mailer:
class MessageMailer < ApplicationMailer
def contact(message)
@body = message.body
mail(to: 'myemail@yopmail.com', subject: 'Nouveau message !')
end
end
My message mailer views (in both html.erb & text.erb):
<%= @body %>
My contact form:
<%= form_for messages_path, url: messages_path do |f| %>
<div class="row">
<div class="col-lg-4 col-md-4 col-sm-12">
<%= f.text_field :name, required: true, class: "form-control", placeholder: "Votre nom" %><br>
</div>
<div class="col-lg-4 col-md-4 col-sm-12">
<%= f.text_field :email, required: true, class: "form-control", placeholder: "Votre email" %><br>
</div>
<div class="col-lg-4 col-md-4 col-sm-12">
<%= f.text_field :phone_number, required: true, class: "form-control", placeholder: "Votre numéro de téléphone" %><br>
</div>
</div>
<%= f.text_area :message,
rows: 8,
cols: 40,
required: true,
class: "form-control",
placeholder: "Votre message"%><br>
<%= f.submit 'Envoyer', class: 'btn btn-secondary' %>
<% end %>
In my config/environment.rb as well as in my config/environments/production.rb & config/environments/development.rb (SENDGRID_LOGIN and SENDGRID_PWD are both in my .env file):
ActionMailer::Base.smtp_settings = {
:user_name => ENV['SENDGRID_LOGIN'],
:password => ENV['SENDGRID_PWD'],
:domain => 'mydomain.fr',
:address => 'smtp.sendgrid.net',
:port => 587,
:authentication => :plain,
:enable_starttls_auto => true
}
I've tried removing the 'require(:message)' in the message_params of my controller --> no error message in my browser, but I'm receiving no email and my terminal says I have unpermitted params:
Started POST "/messages" for ::1 at 2020-12-30 16:02:17 +0100
Processing by MessagesController#create as HTML
Parameters: {"utf8"=>"?", "authenticity_token"=>"x+dJYWSNThRzlmLBj9KrUD1ZEkzX1wa67jvOvwNh4vKgyHQcpqC3e/gjM6/rwc5cVX609GtvnO0I5o60usmeJA==", "/messages"=>{"name"=>"Coline", "email"=>"test@yopmail.com", "phone_number"=>"0606060606", "message"=>"jhwsdiufhsukjhdfkdbvf"}, "commit"=>"Envoyer"}
Unpermitted parameters: :utf8, :authenticity_token, :/messages, :commit
Redirected to http://localhost:3000/
Completed 302 Found in 5ms (ActiveRecord: 0.0ms)