Published by dan on Feb 21, 2015


Filed under Features

This article builds on top of the Mailing Lists functionality.

If you're sending emails then you should always allow recipients to opt-out, and many countries have written the practice into law. Most email services provide unsubscribe functionality, but using it results in the user unsubscribing from all future emails, not just ones from a certain group.

Allowing a user to opt-out of a group's emails with one click is easy:

# config/routes.rb
get 'subscriptions/:id/unsubscribe', to: 'subscriptions#destroy', as: 'unsubscribe'
# app/controllers/subscriptions_controller.rb
class SubscriptionsController < ApplicationController
  def destroy
    # the membership token uniquely identifies the membership
    # without being easy for others to guess.
    @membership = Membership.where(token: params[:id]).first

    if @membership
      @membership.receives_every_message = false
      @membership.receives_digest = false false)
<!-- app/views/subscriptions/destroy.html.erb -->
<p>You've unsubscribed from this group's emails.</p>
<!-- Add this the bottom of app/views/groups_mailer/new_message.html.erb -->
<p><%= link_to 'Unsubscribe', unsubscribe_url(id: @membership.token) %></p>