LoginSignup
0
0

More than 3 years have passed since last update.

Rails チュートリアル 第11章 学習内容のメモ

Posted at

第11章アカウントの有効化

  • 本章ではユーザの新規登録時に、該当のユーザが本当にそのメールアドレスの持ち主であることを確認する機能を追加する

Activationトークンのコールバック

  • 6章で実装したbefore_saveコールバックではオブジェクトが保存される直前、オブジェクトの作成時や更新時にそのコールバックが呼び出される
  • 今回はオブジェクトの作成時のみにコールバックを呼び出したいので、before_createコールバックを使用する
  • 以下のように実装する。
app/models/user.rb
class User < ApplicationRecord
  attr_accessor :remember_token, :activation_token
  before_create :create_activation_digest
  validates :name,  presence: true, length: { maximum: 50 }
  .
  .
  .
  private

    # 有効化トークンとダイジェストを作成および代入する
    def create_activation_digest
      self.activation_token  = User.new_token
      self.activation_digest = User.digest(activation_token)
    end
end

アカウント有効化のメール送信

  • 有効化メールを送信するための、メイラーを作成する
  • メイラーの構成は、コントローラーのアクションとよく似ており、メールのテンプレートをビューと同じように定義できる
  • メイラーはrails generateで生成できる

本番環境でのメール送信

  • テストはパスしていたが、ユーザ登録の際に送付されたURLをクリックした際に以下のページが表示された

image.png

  • 本番環境の設定を誤っていた
  • 以下の'<your heroku app>.herokuapp.com'の箇所を自身のherokuアプリのURLに変更することで解決
config/environments/production.rb
Rails.application.configure do
  .
  .
  .
  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.delivery_method = :smtp
  host = '<your heroku app>.herokuapp.com'
  config.action_mailer.default_url_options = { host: host }
  ActionMailer::Base.smtp_settings = {
    :address        => 'smtp.sendgrid.net',
    :port           => '587',
    :authentication => :plain,
    :user_name      => ENV['SENDGRID_USERNAME'],
    :password       => ENV['SENDGRID_PASSWORD'],
    :domain         => 'heroku.com',
    :enable_starttls_auto => true
  }
  .
  .
  .
end

メモ

- rails serverを立ち上げようとすると、以下のように表示され起動できなくなった

$ rails s
=> Booting WEBrick
=> Rails 3.2.14 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
A server is already running. Check /worktest/sample/tmp/pids/server.pid.
Exiting
  • バックグラウンドでrails serverが立ち上がっていることが原因
  • $ ps aux | grep pumaでrails serverプロセスを検索し、
  • kill -9 ”プロセスID”でプロセスを停止しようとするができない
  • -bash: kill: (37580) - No such processと表示される
  • プロセスIDが自動で切り替わるバグが発生している??
  • 以下のURLを参考にして対処した

Rails sのプロセスが切れない時

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0