diff --git a/Gemfile b/Gemfile index 35e0a556..ce89c049 100644 --- a/Gemfile +++ b/Gemfile @@ -5,6 +5,7 @@ gem 'rails', '6.0.4.1' gem 'rake', '>= 10' gem 'pg' gem 'sprockets-rails', require: 'sprockets/railtie' +gem 'mail_form' gem 'bootsnap' gem 'devise' diff --git a/Gemfile.lock b/Gemfile.lock index 45b4de5e..cf3af0a1 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -166,6 +166,9 @@ GEM nokogiri (>= 1.5.9) mail (2.7.1) mini_mime (>= 0.1.1) + mail_form (1.9.0) + actionmailer (>= 5.2) + activemodel (>= 5.2) marcel (1.0.2) method_source (1.0.0) mime-types (3.3.1) @@ -336,6 +339,7 @@ DEPENDENCIES jquery-rails json listen + mail_form newrelic_rpm nokogiri (>= 1.12.5) oauth diff --git a/app/controllers/contact_controller.rb b/app/controllers/contact_controller.rb index fc4eb5d4..1bf56afb 100644 --- a/app/controllers/contact_controller.rb +++ b/app/controllers/contact_controller.rb @@ -1,18 +1,24 @@ class ContactController < ApplicationController + before_action :contact_params, only: [:create] + + def index + @contact = ContactForm.new + end + def create - @contact = Contact.new(contact_params) - if @contact.invalid? - messages = @contact.errors.full_messages.to_sentence - render json: { errors: messages }, status: :unprocessable_entity + @contact = ContactForm.new(params[:contact_form]) + @contact.request = request + if @contact.deliver + flash.now[:notice] = 'Your feedback has been submitted. We will reply as soon as possible.' + render :index else - ContactMailer.contact_email(@contact).deliver - render json: { message: 'Your feedback has been submitted. We will reply as soon as possible.' } + flash.now[:error] = 'Cannot send message.' + render :index end - end - + end + private - def contact_params - params.require(:contact).permit(:email, :message, :name, :subject) + params.require(:contact_form).permit(:email, :message, :name, :subject) end -end +end \ No newline at end of file diff --git a/app/controllers/pages_controller.rb b/app/controllers/pages_controller.rb index 12884648..29469101 100644 --- a/app/controllers/pages_controller.rb +++ b/app/controllers/pages_controller.rb @@ -2,5 +2,6 @@ class PagesController < ApplicationController def index @upcoming_meetings = upcoming_meetings @next_meeting = next_meeting + @contact = contact_params end end diff --git a/app/javascript/create.js.erb b/app/javascript/create.js.erb new file mode 100644 index 00000000..1dc51da9 --- /dev/null +++ b/app/javascript/create.js.erb @@ -0,0 +1,7 @@ +// app/views/home/create.js.erb + +// Test for ajax success +console.log("This is the create.js.erb file"); +// Render flash message +$('#contact').html("<%= j render 'contact_form' %>"); +$('#flash-message').html("<%= j render 'flash' %>").delay(3000).fadeOut(4000); \ No newline at end of file diff --git a/app/mailers/contact_mailer.rb b/app/mailers/contact_mailer.rb index 920ede25..588104cc 100644 --- a/app/mailers/contact_mailer.rb +++ b/app/mailers/contact_mailer.rb @@ -1,8 +1,24 @@ -class ContactMailer < ActionMailer::Base +class ContactForm < MailForm::Base default :to => "cincinnatirb@googlegroups.com" + attribute :name, :validate => true + attribute :email, :validate => /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i + attribute :message + attribute :nickname, :captcha => true + def contact_email(contact) @contact = contact mail(:from => @contact.email, :subject => "Feedback from cincyrb.com") end + + # Declare the e-mail headers. It accepts anything the mail method + # in ActionMailer accepts. + def headers + { + :subject => "Contact Form Inquiry", + :to => "your_email@your_domain.com", + :from => %("#{name}" <#{email}>) + } + end + end diff --git a/app/models/contact.rb b/app/models/contact.rb deleted file mode 100644 index 9b2f7ec9..00000000 --- a/app/models/contact.rb +++ /dev/null @@ -1,22 +0,0 @@ -class Contact - include ActiveModel::Validations - include ActiveModel::Conversion - extend ActiveModel::Naming - - attr_accessor :name, :email, :subject, :message - - validates_presence_of :name, :subject, :message - validates_format_of :email, :with => /\A[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}\z/i - validates_length_of :subject, :maximum => 100 - validates_length_of :message, :maximum => 1000 - - def initialize(attributes = {}) - attributes.each do |name, value| - send("#{name}=", value) - end - end - - def persisted? - false - end -end diff --git a/app/views/pages/_contact_form.html.erb b/app/views/pages/_contact_form.html.erb new file mode 100644 index 00000000..6cb81e2d --- /dev/null +++ b/app/views/pages/_contact_form.html.erb @@ -0,0 +1,26 @@ +<%= form_for @contact, url: pages_index_path, remote: true do |f| %> + + +
Have a topic idea? Want to present at a Cinci.rb meeting? Have a suggestion? Send us a comment here.
+ + <%= form_for Contact.new, url: "/pages", :remote => true do |f| %> +