From f8c91add3e8b7760028b0c18b04052f01c270316 Mon Sep 17 00:00:00 2001 From: rebuilt Date: Tue, 1 Nov 2022 17:06:01 -0700 Subject: [PATCH] Update contact form so it confirms the message was sent --- app/controllers/home_controller.rb | 15 +++++++++++++++ app/models/contact.rb | 12 ++++++------ .../{_new.html.erb => _contact_form.html.erb} | 2 +- app/views/contacts/new.html.erb | 2 +- app/views/home/index.html.erb | 2 +- app/views/layouts/application.html.erb | 1 - config/routes.rb | 1 + 7 files changed, 25 insertions(+), 10 deletions(-) rename app/views/contacts/{_new.html.erb => _contact_form.html.erb} (90%) diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index ff3df1a..92b789d 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -1,5 +1,20 @@ class HomeController < ApplicationController + skip_before_action :verify_authenticity_token def index @contact = Contact.new end + + def create + puts '***************************** Inside home controller *****************************************' + + flash.now[:success] = 'Message sent!' + @contact = Contact.new(params[:contact]) + @contact.request = request + if @contact.deliver + flash.now[:success] = 'Message sent!' + else + flash.now[:error] = 'Could not send message' + end + render :index + end end diff --git a/app/models/contact.rb b/app/models/contact.rb index c507a33..fc46a4f 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -1,17 +1,17 @@ class Contact < MailForm::Base attribute :name, validate: true - attribute :email, validate: /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i + attribute :email, validate: /\A([\w.%+\-]+)@([\w\-]+\.)+(\w{2,})\z/i attribute :organization - attribute :message - attribute :nickname, captcha: true + attribute :message, validate: true + attribute :nickname, captcha: true # Declare the e-mail headers. It accepts anything the mail method # in ActionMailer accepts. def headers { - :subject => "Contact Form Inquiry", - :to => ENV['CONTACT_FORM_DESTINATION'] || "memoryman51@hotmail.com", - :from => %("#{name}" <#{email}>) + subject: 'Contact Form Inquiry', + to: ENV['CONTACT_FORM_DESTINATION'] || 'memoryman51@hotmail.com', + from: %("#{name}" <#{email}>) } end end diff --git a/app/views/contacts/_new.html.erb b/app/views/contacts/_contact_form.html.erb similarity index 90% rename from app/views/contacts/_new.html.erb rename to app/views/contacts/_contact_form.html.erb index 4dc9b6e..e52a3ea 100644 --- a/app/views/contacts/_new.html.erb +++ b/app/views/contacts/_contact_form.html.erb @@ -1,6 +1,6 @@

Contact Form

- <%= form_for @contact do |f| %> + <%= form_for @contact, url: home_index_path, method: :create, data: { turbo: false } do |f| %>
<%= f.label :name %>
<%= f.text_field :name, required: true, class: "contact-form-text-area" %>
diff --git a/app/views/contacts/new.html.erb b/app/views/contacts/new.html.erb index c2f71f8..7fb90a5 100644 --- a/app/views/contacts/new.html.erb +++ b/app/views/contacts/new.html.erb @@ -1 +1 @@ -<%= render(partial: "new") %> +<%= render(partial: "contacts/contact_form") %> diff --git a/app/views/home/index.html.erb b/app/views/home/index.html.erb index 4a4c905..67346b4 100644 --- a/app/views/home/index.html.erb +++ b/app/views/home/index.html.erb @@ -40,4 +40,4 @@ <%# <%= link_to "Contact Form", new_contact_path %1> %> -<%= render partial: "contacts/new" %> +<%= render partial: "contacts/contact_form" %> diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 99c0eae..9f1935c 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -13,7 +13,6 @@ <%= render partial: 'layouts/nav' %> - <%= yield %> diff --git a/config/routes.rb b/config/routes.rb index 99cf864..61f429b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -8,6 +8,7 @@ Rails.application.routes.draw do resources :work_with_ecp, only: :index resources :construction, only: :index resources :district_leader, only: :index + resources :home, only: %i[index create] resources :contacts, only: %i[new create]