Ruby on Railsの開発環境で送信したメールをブラウザで確認するためにgem ‘letter_opener_web’を使ってみます。上の画像のようなページを簡単に導入できて、メールの確認ができます。本記事ではメール送信をdeviseのconfirmableモジュールを使用してsign_up時に行ってみました。
letter_opener_web
https://github.com/fgrehm/letter_opener_web
devise
https://github.com/plataformatec/devise
実行環境はこちらです
1 2 3 4 5 |
ruby 2.4.0 rails 5.1.6 devise 4.5.0 letter_opener 1.6.0 letter_opener_web 1.3.4 |
準備
新規アプリ作成し、scaffoldで足場を作っていきます。
1 2 3 |
$ rails new letter_opener_web $ cd letter_opener_web $ rails g scaffold User name:string |
Gemの追加
gemfileに以下のように追加します。
Gemfile
1 2 3 4 5 6 7 |
gem 'devise' # ~ 省略 ~ group :development do gem 'letter_opener_web', '~> 1.0' end |
Devise
bundle installしてdeviseも導入します。
1 2 3 4 5 |
$ bundle install $ rails g devise:install $ rails g devise user $ rails g devise:views users |
confirmableを追加することで、確認メールを送信できます。
app/models/user.rb
1 2 3 4 5 |
class User < ApplicationRecord devise :database_authenticatable, :registerable, :confirmable, :recoverable, :rememberable, :validatable end |
マイグレーションファイルの中の、Confirmable関連のコメントを外します。
db/migrate/20180928034759_add_devise_to_users.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# frozen_string_literal: true class DeviseCreateUsers < ActiveRecord::Migration[5.1] def change create_table :users do |t| ## Database authenticatable t.string :email, null: false, default: "" t.string :encrypted_password, null: false, default: "" ## Recoverable t.string :reset_password_token t.datetime :reset_password_sent_at ## Rememberable t.datetime :remember_created_at ## Trackable # t.integer :sign_in_count, default: 0, null: false # t.datetime :current_sign_in_at # t.datetime :last_sign_in_at # t.string :current_sign_in_ip # t.string :last_sign_in_ip ## Confirmable t.string :confirmation_token t.datetime :confirmed_at t.datetime :confirmation_sent_at t.string :unconfirmed_email # Only if using reconfirmable ## Lockable # t.integer :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts # t.string :unlock_token # Only if unlock strategy is :email or :both # t.datetime :locked_at t.timestamps null: false end add_index :users, :email, unique: true add_index :users, :reset_password_token, unique: true add_index :users, :confirmation_token, unique: true # add_index :users, :unlock_token, unique: true end end |
1 |
$ rails db:migrate |
Routeing
/letter_openerでLetterOpenerWeb::Engineをマウントします。
1 2 3 4 5 6 7 |
Rails.application.routes.draw do devise_for :users resources :users mount LetterOpenerWeb::Engine, at: "/letter_opener" if Rails.env.development? end |
Config
以下のようにデフォルトURL指定します。config.action_mailer.delivery_methodは:letter_opener_webに変更します。ちなみに開発環境がcloud9の場合は{ host: ‘myapp.c9users.io/’, port: $PORT, protocol: ‘https’ }とするとOKでした。
config/environments/development.rb
1 2 3 4 5 6 |
Rails.application.configure do config.action_mailer.default_url_options = { host: 'localhost:3000' } config.action_mailer.delivery_method = :letter_opener_web end |
これで http://localhost:3000/users/sign_up からアカウントを作成し、送信されたメールを http://localhost:3000/letter_opener で確認することが出来るようになります!
以上です!ご覧いただき、ありがとうございました!!