Skip to content

RailsApps/rails-mailinglist-activejob

Repository files navigation

Rails Mailing List Active Job Rails Mailing List with Active Job

Rails 5.0 example application using Active Job for background processing. The application lets a visitor join a MailChimp-hosted mailing list.

Use this example application as a starter app for your own web applications. For a complete explanation of the code, see the tutorial:

You can build this application in only a few minutes using the Rails Composer tool, choosing either a Bootstrap or Foundation front-end framework, as well as many other options, such as Haml or Slim.

Join RailsApps

From the RailsApps Project

The RailsApps open source project offers starter applications and tutorials for Rails developers. Generate the applications with the Rails Composer tool.

All the code is explained in the Capstone Rails Tutorials. You can purchase the Capstone Rails Tutorials to support the project.

If You Are New to Rails

If you’re new to Rails, see What is Ruby on Rails?, the book Learn Ruby on Rails, and recommendations for a Rails tutorial.

What Is Implemented — and What Is Not

This application extends the rails-bootstrap example application, showing how to use Active Job for background processing.

The application can be used for a simple web application that encourages a visitor to sign up for a mailing list. Asking for an email address gives the site owner a way to stay in touch with visitors. The application uses Active Job to process a task in the background, for a faster and more responsive user experience. Active Job, introduced in Rails 4.2, is useful for any application that connects to an external server.

The tutorial provides additional code, showing how to:

  • use landing pages to segment a mailing list
  • record referrer data to improve marketing efforts

These additional features are not related to background processing but they are useful if you are signing up visitors for a mailing list.

Database

The application requires a database. The example application uses SQLite with Rails ActiveRecord. You can easily substitute PostgreSQL, MySQL, or other databases.

Front-end Framework

The example application (here in the GitHub repository) integrates Bootstrap for a navigation bar and flash messages. The rails_layout gem is included so you can switch to the Foundation front-end framework.

Similar Examples and Tutorials

This is one in a series of Rails example apps and tutorials from the RailsApps Project. See a list of additional Rails examples, tutorials, and starter apps. Related example applications may be useful:

Accounts You Will Need

You’ll need a MailChimp account.

When visitors submit an email address, the application will add them to a MailChimp list. To access MailChimp, we’ll need a MailChimp API key. “Log in to MailChimp”https://admin.mailchimp.com/ to get your API key. Click your name at the top of the navigation menu, then click “Account”. Click “Extras,” then “API keys.” You have to generate an API key; MailChimp doesn’t create one automatically.

You’ll also need to create a MailChimp mailing list. The MailChimp “Lists” page has a button for “Create List.” The list name and other details are up to you. We’ll need the MAILCHIMP_LIST_ID for the mailing list you’ve created. To find the list ID, on the MailChimp “Lists” page, click the “down arrow” for a menu and click “Settings.” At the bottom of the “List Settings” page, you’ll find the unique ID for the mailing list.

With MailChimp, you can send a welcome message automatically when the visitor signs up for the mailing list. Use the welcome message to inform the visitor that they’ve successfully subscribed to the mailing list. It’s a bit difficult to find the MailChimp option to create a welcome message. Strangely, MailChimp considers a welcome message a “form.” Here’s how to find it. On the MailChimp “Lists” page, click the “down arrow” for a list and click “Signup forms.” Then click “General forms.” On the “Create Forms” page, there is a drop-down list of “Forms & Response Emails.” The gray box shows “Signup form.” Click the down arrow. Select the menu item named “Final ‘Welcome’ Email” and you’ll be able to create a welcome message.

We provide instructions to deploy the application to Heroku which provides Rails application hosting. It costs nothing to set up a Heroku account and deploy as many applications as you want. To deploy an app to Heroku, you must have a Heroku account. Visit Heroku to set up an account.

Dependencies

Before generating your application, you will need:

  • The Ruby language – version 2.3.1
  • The Rails gem – version 5.0

See the article Installing Rails for instructions about setting up Rails and your development environment. See the article Updating to Rails 5.0 if you are using an earlier version of Rails.

Getting the Application

Local

You have several options for getting the code on your own machine. You can fork, clone, or generate.

Fork

If you’d like to add features (or bug fixes) to improve the example application, you can fork the GitHub repo and make pull requests. Your code contributions are welcome!

Clone

If you want to copy and customize the app with changes that are only useful for your own project, you can clone the GitHub repo. You’ll need to search-and-replace the project name throughout the application. You probably should generate the app instead (see below). To clone:

$ git clone git://github.com/RailsApps/rails-mailinglist-activejob.git

You’ll need git on your machine. See Rails and Git.

Generate

If you want to use the project as a starter application, use the Rails Composer tool to generate a new version of the example app. You’ll be able to give it your own project name when you generate the app. Generating the application gives you additional options.

To build the example application, Rails 5.0 must be installed in your development environment. Run the command:

$ rails new rails-mailinglist-activejob -m https://raw.github.com/RailsApps/rails-composer/master/composer.rb

The $ character indicates a shell prompt; don’t include it when you run the command.

This creates a new Rails app named rails-mailinglist-activejob on your computer. You can use a different name if you wish.

You’ll see a prompt:

option  Build a starter application?
    1)  Build a RailsApps example application
    2)  Contributed applications
    3)  Custom application

Enter “1” to select Build a RailsApps example application. You’ll see a prompt:

option  Choose a starter application.
    1)  learn-rails
    2)  rails-bootstrap
    3)  rails-foundation
    4)  rails-mailinglist-activejob
    5)  rails-omniauth
    6)  rails-devise
    7)  rails-devise-roles
    8)  rails-devise-pundit
    9)  rails-signup-download
   10)  rails-stripe-checkout

Choose rails-mailinglist-activejob. The Rails Composer tool may give you other options (other applications may have been added since these notes were written).

The application generator template will ask you for additional preferences:

option  Web server for development?
     1)  WEBrick (default)
     2)  Thin
     3)  Unicorn
     4)  Puma
     5)  Phusion Passenger (Apache/Nginx)
     6)  Phusion Passenger (Standalone)
 option  Web server for production?
     1)  Same as development
     2)  Thin
     3)  Unicorn
     4)  Puma
     5)  Phusion Passenger (Apache/Nginx)
     6)  Phusion Passenger (Standalone)
 option  Database used in development?
     1)  SQLite
     2)  PostgreSQL
     3)  MySQL
 option  Template engine?
     1)  ERB
     2)  Haml
     3)  Slim
 option  Test framework?
     1)  None
     2)  RSpec with Capybara
 option  Continuous testing?
     1)  None
     2)  Guard
 option  Front-end framework?
     1)  None
     2)  Bootstrap 3.2
     3)  Bootstrap 2.3
     4)  Zurb Foundation 5.4
     5)  Zurb Foundation 4.0
     6)  Simple CSS
 option  Add support for sending email?
     1)  None
     2)  Gmail
     3)  SMTP
     4)  SendGrid
     5)  Mandrill
 option  Install page-view analytics?
     1)  None
     2)  Google Analytics
     3)  Segment.io
 option  Prepare for deployment?
     1)  no
     2)  Heroku
     3)  Capistrano
 option  Set a robots.txt file to ban spiders? (y/n) n
 option  Create a GitHub repository? (y/n) n
 option  Use or create a project-specific rvm gemset? (y/n) y

Web Servers

If you plan to deploy to Heroku, select Unicorn as your production webserver. Unicorn is recommended by Heroku.

Database

Use SQLite for development on Mac or Linux, unless you already have PostgreSQL installed locally. Use PostgreSQL if you plan to deploy to Heroku. You can easily change the database later if you select SQLite to start.

Template Engine

The example application uses the default “ERB” Rails template engine. Optionally, you can use another template engine, such as Haml or Slim. See instructions for Haml and Rails.

Testing

If you are a beginner, select “None.”

Front-end Framework

The example in the GitHub repository was built with the Bootstrap 3 front-end framework. Use Zurb Foundation 5.5 if you like. Choosing either Bootstrap or Foundation will automatically install views with attractive styling.

Email

The example application does not send email to users, so you do not need to add support for sending email.

Other Choices

Set a robots.txt file to ban spiders if you want to keep your new site out of Google search results.

If you choose to create a GitHub repository, the generator will prompt you for a GitHub username and password.

It is a good idea to use RVM, the Ruby Version Manager, and create a project-specific RVM gemset (not available on Windows). See Installing Rails.

Troubleshooting

If you get an error “OpenSSL certificate verify failed” or “Gem::RemoteFetcher::FetchError: SSL_connect” see the article OpenSSL errors and Rails.

Edit the README

If you’re storing the app in a GitHub repository, please edit the README files to add a description of the app and your contact info. If you don’t change the README, people will think I am the author of your version of the application.

Getting Started

See the article Installing Rails to make sure your development environment is prepared properly.

Use RVM

I recommend using RVM, the Ruby Version Manager, to create a project-specific gemset for the application. If you generate the application with the Rails Composer tool, you can create a project-specific gemset.

Gems

Here are the gems used by the application:

These gems make development easier:

Your choice of front-end framework:

Install the Required Gems

If you used the Rails Composer tool to generate the example app, the application template script has already run the bundle install command.

If not, you should run the bundle install command to install the required gems on your computer:

$ bundle install

You can check which gems are installed on your computer with:

$ gem list

Keep in mind that you have installed these gems locally. When you deploy the app to another server, the same gems (and versions) must be available.

Front-end Framework

If you generate the application using the Rails Composer tool, you have the option to install either Bootstrap or Foundation.

Changing the Front-end Framework

The version of the application in the repository includes Bootstrap. If you wish to install Foundation instead, use the rails_layout gem to generate new files. First add a gem to the Gemfile:

gem 'foundation-rails'

Use Bundler to install the gem:

$ bundle install

To create layout files for use with Zurb Foundation 5.5:

$ rails generate layout:install foundation5

Configuration File

To consolidate configuration settings in a single location, we store credentials in the config/secrets.yml file. To keep your credentials private, use Unix environment variables to set your credentials. See the article Rails Environment Variables for more information.

Add your credentials to the file config/secrets.yml:

# Make sure the secrets in this file are kept private
# if you're sharing your code publicly.

development:
  domain_name: example.com
  mailchimp_api_key: <%= ENV["MAILCHIMP_API_KEY"] %>
  mailchimp_list_id: <%= ENV["MAILCHIMP_LIST_ID"] %>
  secret_key_base: some_long_string

test:
  secret_key_base: some_long_string

# Do not keep production secrets in the repository,
# instead read values from the environment.
production:
  domain_name: <%= ENV["DOMAIN_NAME"] %>
  mailchimp_api_key: <%= ENV["MAILCHIMP_API_KEY"] %>
  mailchimp_list_id: <%= ENV["MAILCHIMP_LIST_ID"] %>
  secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>

All configuration values in the config/secrets.yml file are available anywhere in the application as variables. For example, Rails.application.secrets.domain_name will return the string example.com.

If you don’t want to use Unix environment variables, you can set each value directly in the config/secrets.yml file. The file must be in your git repository when you deploy to Heroku. However, you shouldn’t save the file to a public GitHub repository where other people can see your credentials.

Database Seed File

The db/seeds.rb file is not needed in this application.

Set the Database

If you’ve used the Rails Composer tool to generate the application, the database is already set up with rails db:migrate.

If you’ve cloned the repo, prepare the database and add the default user to the database by running the command:

$ rails db:migrate

Use rails db:reset if you want to empty and reseed the database.

Change your Application’s Secret Token

If you’ve used the Rails Composer tool to generate the application, the application’s secret token will be unique, just as with any Rails application generated with the rails new command.

However, if you’ve cloned the application directly from GitHub, it is crucial that you change the application’s secret token before deploying your application in production mode. Otherwise, people could change their session information, and potentially access your site without permission. Your secret token should be at least 30 characters long and completely random.

Get a unique secret token:

rails secret

Edit the config/secrets.yml file to change the secret token.

Test the App

You can check that your application runs properly by entering the command:

$ rails server

To see your application in action, open a browser window and navigate to http://localhost:3000/.

You’ll see a home page with a sign-up form.

Enter your email address and click the “sign up” button. You should see the page redisplay with an acknowledgment message. Try entering an invalid email address such as “me@foo@”, or click the submit button without entering an email address, and you should see an error message.

You can log in to MailChimp and check your list of subscribers to see if the new email address was added successfully.

Stop the server with Control-C. If you test the app by starting the web server and then leave the server running while you install new gems, you’ll have to restart the server to see any changes. The same is true for changes to configuration files in the config folder. This can be confusing to new Rails developers because you can change files in the app folders without restarting the server. Stop the server each time after testing and you will avoid this issue.

RSpec Test Suite

The application contains a suite of RSpec tests. To run:

$ rspec

Deploy to Heroku

Heroku provides low cost, easily configured Rails application hosting.

You can deploy from the command line.

$ git push origin master

If you’ve set configuration values in the config/secrets.yml file, you’ll need to set them as Heroku environment variables. You can set Heroku environment variables directly with heroku config:add. For example:

$ heroku config:add MAILCHIMP_API_KEY='a6v34ggf23c123098765fcc6c996c540-us2' MAILCHIMP_LIST_ID='4x8bfgb034'

Complete Heroku deployment with:

$ git push heroku master

See the Tutorial for Rails on Heroku for details.

Troubleshooting

Problems? Check the issues.

Issues

Please create a GitHub issue if you identify any problems or have suggestions for improvements.

Where to Get Help

Your best source for help with problems is Stack Overflow. Your issue may have been encountered and addressed by others.

Use the tag “railsapps” on Stack Overflow for extra attention.

Contributing

If you make improvements to this application, please share with others.

Send the author a message, create an issue, or fork the project and submit a pull request.

If you add functionality to this application, create an alternative implementation, or build an application that is similar, please contact me and I’ll add a note to the README so that others can find your work.

Credits

Daniel Kehoe implemented the application and wrote the tutorial.

Is the app useful to you? Follow the project on Twitter: @rails_apps
and tweet some praise. I’d love to know you were helped out by what I’ve put together.

MIT License

MIT License

Copyright ©2014-16 Daniel Kehoe

Useful Links

Getting Started Articles Tutorials
Ruby on Rails Analytics for Rails Rails Bootstrap
What is Ruby on Rails? Heroku and Rails Rails Foundation
Learn Ruby on Rails JavaScript and Rails RSpec Tutorial
Rails Tutorial Rails Environment Variables Rails Devise Tutorial
Ruby on Rails Tutorial for Beginners Git and GitHub with Rails Devise RSpec
Install Ruby on Rails Send Email with Rails Devise Bootstrap
Install Ruby on Rails – Mac OS X Haml and Rails Rails Membership Site with Stripe
Install Ruby on Rails – Ubuntu Rails Application Layout Rails Subscription Site with Recurly
Ruby on Rails – Nitrous.io HTML5 Boilerplate for Rails Startup Prelaunch Signup Application
Update Rails Example Gemfiles for Rails
Rails Composer Rails Application Templates
Rails Examples Rails Product Planning
Rails Starter Apps Rails Project Management

About

Rails 5.0 starter app using Active Job for background processing.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published