Published by Dan Cunning on Feb 21, 2015
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
return unless @membership
@membership.receives_every_message = false
@membership.receives_digest = false
@membership.save(validate: false)
end
end
<!-- 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>
I'm a Ruby on Rails contractor from Atlanta GA, focusing on simplicity and usability through solid design. Read more »