Skip to content

Commit

Permalink
mailing list
Browse files Browse the repository at this point in the history
  • Loading branch information
crwhitesides committed Oct 8, 2014
1 parent 3c98bc7 commit 654d594
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 29 deletions.
23 changes: 20 additions & 3 deletions app/controllers/visitors_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
class VisitorsController < ApplicationController

def new
@owner = Owner.new
end
def new
@visitor = Visitor.new
end

def create
@visitor = Visitor.new(secure_params)
if @visitor.valid?
@visitor.subscribe
flash[:notice] = "Signed up #{@visitor.email}."
redirect_to root_path
else
render :new
end
end

private

def secure_params
params.require(:visitor).permit(:email)
end

end
21 changes: 0 additions & 21 deletions app/models/owner.rb

This file was deleted.

19 changes: 19 additions & 0 deletions app/models/visitor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Visitor < ActiveRecord::Base
has_no_table
column :email, :string
validates_presence_of :email
validates_format_of :email, :with => /\A[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}\z/i

def subscribe
mailchimp = Gibbon::API.new(Rails.application.secrets.mailchimp_api_key)
result = mailchimp.lists.subscribe({
:id => Rails.application.secrets.mailchimp_list_id,
:email => {:email => self.email},
:double_optin => false,
:update_existing => true,
:send_welcome => true
})
Rails.logger.info("Subscribed #{self.email} to MailChimp") if result
end

end
25 changes: 20 additions & 5 deletions app/views/visitors/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
<h3>Home</h3>
<p>Welcome to the home of <%= @owner.name %>.</p>
<p>I was born on <%= @owner.birthdate %>.</p>
<p>Only <%= @owner.countdown %> days until my birthday!</p>

<% content_for :title do %>Foobar Kadigan<% end %>
<% content_for :description do %>Website of Foobar Kadigan<% end %>
<section>
<img src="https://lorempixel.com/1170/600/cats/1">
</section>
<section>
<div class="column">
<h2>Stay in touch.</h2>
</div>
<div class="column">
<div class="form-centered">
<%= simple_form_for @visitor do |f| %>
<%= f.error_notification %>
<%= f.input :email, label: false, :placeholder => 'Your email address...' %>
<br/>
<%= f.button :submit, "Sign up for the newsletter", :class => "submit" %>
<% end %>
</div>
</div>
</section>
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Rails.application.routes.draw do
resources :contacts, only: [:new, :create]
resources :visitors, only: [:new, :create]
root to: 'visitors#new'
end

0 comments on commit 654d594

Please sign in to comment.