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| %> + + +
+

Contact Us

+

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| %> +
+
+ <%= f.text_field :name, :placeholder => "Name" %> + <%= f.text_field :email, :placeholder => "Email Address" %> + <%= f.text_field :subject, :placeholder => "Subject" %> +
+
+ <%= f.text_area :message, :placeholder => "Message", :rows => 5 %> +
+
+ +
+
+ <%- end %> +
+ <%= render 'flash' %> +
+
\ No newline at end of file diff --git a/app/views/pages/_flash.html.erb b/app/views/pages/_flash.html.erb new file mode 100644 index 00000000..4a6a0dd7 --- /dev/null +++ b/app/views/pages/_flash.html.erb @@ -0,0 +1,3 @@ +<% flash.each do |message_type, message| %> + <%= content_tag(:div, message, class: "alert alert-#{message_type}") %> +<% end %> \ No newline at end of file diff --git a/config/environments/development.rb b/config/environments/development.rb index 66df51f6..123f6fef 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -59,4 +59,19 @@ # Use an evented file watcher to asynchronously detect changes in source code, # routes, locales, etc. This feature depends on the listen gem. config.file_watcher = ActiveSupport::EventedFileUpdateChecker + + + # config/environments/development.rb + + config.action_mailer.perform_deliveries = true + config.action_mailer.raise_delivery_errors= true + config.action_mailer.delivery_method = :smtp + config.action_mailer.smtp_settings = { + address: 'smtp.gmail.com', + port: 587, + domain: 'gmail.com', + user_name: ENV["GMAIL_EMAIL"], + password: ENV["GMAIL_PASSWORD"], + authentication: 'plain', + enable_starttls_auto: true } end diff --git a/config/routes.rb b/config/routes.rb index 1fa88773..311a6570 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,7 +4,8 @@ ActiveAdmin.routes(self) - post 'contact_us' => 'contact#create' + resources :pages, only: [:index, :new, :create] + get 'meetings' => 'meetings#index' get 'twitter/timeline' diff --git a/vendor/cache/mail_form-1.9.0.gem b/vendor/cache/mail_form-1.9.0.gem new file mode 100644 index 00000000..9a4205a2 Binary files /dev/null and b/vendor/cache/mail_form-1.9.0.gem differ