Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Flash messages are an anti-pattern

Claudio B.
January 14, 2016

Flash messages are an anti-pattern

Claudio B.

January 14, 2016
Tweet

More Decks by Claudio B.

Other Decks in Programming

Transcript

  1. $ rails new club $ cd club $ rails g

    scaffold member name age:integer $ rake db:create db:migrate && rails s What are flash messages?
  2. $ rails new club $ cd club $ rails g

    scaffold member name age:integer $ rake db:create db:migrate && rails s What are flash messages?
  3. $ rails new club $ cd club $ rails g

    scaffold member name age:integer $ rake db:create db:migrate && rails s What are flash messages?
  4. $ rails new club $ cd club $ rails g

    scaffold member name age:integer $ rake db:create db:migrate && rails s What are flash messages?
  5. $ rails new club $ cd club $ rails g

    scaffold member name age:integer $ rake db:create db:migrate && rails s What are flash messages?
  6. $ rails new club $ cd club $ rails g

    scaffold member name age:integer $ rake db:create db:migrate && rails s What are flash messages?
  7. $ rails new club $ cd club $ rails g

    scaffold member name age:integer $ rake db:create db:migrate && rails s What are flash messages?
  8. $ rails new club $ cd club $ rails g

    scaffold member name age:integer $ rake db:create db:migrate && rails s What are flash messages?
  9. $ rails new club $ cd club $ rails g

    scaffold member name age:integer $ rake db:create db:migrate && rails s What are flash messages?
  10. $ rails new club $ cd club $ rails g

    scaffold member name age:integer $ rake db:create db:migrate && rails s What are flash messages?
  11. $ rails new club $ cd club $ rails g

    scaffold member name age:integer $ rake db:create db:migrate && rails s What are flash messages?
  12. $ rails new club $ cd club $ rails g

    scaffold member name age:integer $ rake db:create db:migrate && rails s What are flash messages?
  13. $ rails new club $ cd club $ rails g

    scaffold member name age:integer $ rake db:create db:migrate && rails s What are flash messages?
  14. What bad can flash messages do? 1.Disappear after reload 2.Distort

    the attention of the user 3.Are target of designers’ minimalism 4.Are not mobile-friendly def create @member = Member.new(member_params) respond_to do |format| if @member.save format.html { redirect_to @member, notice: 'Member was successfully created.' } format.json { render :show, status: :created} […]
  15. What bad can flash messages do? 1.Disappear after reload 2.Distort

    the attention of the user 3.Are target of designers’ minimalism 4.Are not mobile-friendly def create @member = Member.new(member_params) respond_to do |format| if @member.save format.html { redirect_to @member, notice: 'Member was successfully created.' } format.json { render :show, status: :created} […]
  16. What bad can flash messages do? 1.Disappear after reload 2.Distort

    the attention of the user 3.Are target of designers’ minimalism 4.Are not mobile-friendly 5.Require hacks to work with Javascript 6.Require I18N in the controller 7.Are not cache-friendly def create @member = Member.new(member_params) respond_to do |format| if @member.save format.html { redirect_to @member, notice: 'Member was successfully created.' } format.json { render :show, status: :created} […]
  17. Symptoms of a problem We are not “sure” of the

    value of the information, so we choose a middle- ground of “showing it only once”. By doing this, we go against the REST paradigms of individual resources and nullipotence of the GET method.
  18. How to avoid flash messages # On successful create redirect_to

    @member, notice: 'Member was successfully created.'
  19. How to avoid flash messages # On successful create redirect_to

    @member, notice: 'Member was successfully created.'
  20. How to avoid flash messages # On successful update redirect_to

    @member, notice: 'Member was successfully updated.'
  21. How to avoid flash messages # On successful update redirect_to

    @member, notice: 'Member was successfully updated.'
  22. How to avoid flash messages # On successful update redirect_to

    @member, notice: 'Member was successfully updated.'
  23. How to avoid flash messages # On successful destroy (from

    index) redirect_to members_url, notice: 'Member was successfully destroyed.'
  24. How to avoid flash messages # On successful destroy (from

    index) redirect_to members_url, notice: 'Member was successfully destroyed.'
  25. How to avoid flash messages # On successful destroy (from

    index) redirect_to members_url, notice: 'Member was successfully destroyed.'
  26. How to avoid flash messages # On successful destroy (from

    show) redirect_to members_url, notice: 'Member was successfully destroyed.’
  27. How to avoid flash messages # On successful destroy (from

    show) redirect_to members_url, notice: 'Member was successfully destroyed.’
  28. How to avoid flash messages # On successful destroy (from

    show) redirect_to members_url, notice: 'Member was successfully destroyed.’ redirect_to :back
  29. How to avoid flash messages # On successful destroy (from

    show) redirect_to members_url, notice: 'Member was successfully destroyed.’ redirect_to :back
  30. How to avoid flash messages # db/migrate/20160114174326_add_status_to_member.rb class AddStatusToMember <

    ActiveRecord::Migration def change add_column :members, :status, :integer, default: 0, null: false end end # app/models/member.rb class Member < ActiveRecord::Base enum status: %i(active inactive) end
  31. How to avoid flash messages # app/controllers/members_controller.rb class MembersController <

    ApplicationController def destroy @member.destroy end def index @members = Member.all end def show end end @member.inactive! Member.active render @member.status
  32. How to avoid flash messages # app/views/members/active.html.erb <p> <strong>Last updated:</strong>

    <%= time_ago_in_words @member.updated_at %> ago </p> <p> <strong>Name:</strong> […] # it’s just show.html.erb renamed # app/views/members/inactive.html.erb <p>This user is no longer active.</p> <%= link_to 'Back', members_path %>
  33. How to avoid flash messages # On successful destroy (from

    show) redirect_to members_url, notice: 'Member was successfully destroyed.’ redirect_to :back
  34. How to avoid flash messages # On successful destroy (from

    show) redirect_to members_url, notice: 'Member was successfully destroyed.’ redirect_to :back
  35. Summing up We give “for granted” flash messages in Rails,

    but reasoning about them can lead to a better user experience. The real minimalism is not to hide flash messages, but to remove the reasons why we need them.
  36. How to resist the temptation # config/application.rb module Club class

    Application < Rails::Application config.middleware.delete ActionDispatch::Flash end end Thanks! ! speakerdeck.com/claudiob