morooka ryosuke  2021/04/08更新

登録完了メール


アプリケーション作成時にかなり手間取ったので、その時の解決方法などを

備忘録として記録

前提

下記URLの記事の手順で実装しております

https://web-camp.io/magazine/archives/19143


下記は上記記事の通り、rails new から新たにアプリケーションを作成しsign_upした時のログ画面

scaffoldでuserモデル等を作成し、メール送信もうまくできている

① 記事を参考にしたアプリケーション


Started POST "/users" for 123.221.22.117 at 2021-04-07 23:35:41 +0000
Cannot render console from 123.221.22.117! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by UsersController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"AdkHlx3no3yF9NiKrza74_PRYOmHfL_mQF_hKA-L-COBVnklDX4IKZwEtDiGlFYO9ge5a6dT40TweY8BORkNNw", "user"=>{"name"=>"Morooka ryosuke", "email"=>"basell.4rm.0823@gmail.com"}, "commit"=>"Create User"}
   (0.1ms)  begin transaction
   app/controllers/users_controller.rb:27
  User Create (1.7ms)  INSERT INTO "users" ("name", "email", "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["name", "Morooka ryosuke"], ["email", "basell.4rm.0823@gmail.com"], ["created_at", "2021-04-07 23:35:41.979013"], ["updated_at", "2021-04-07 23:35:41.979013"]]
   app/controllers/users_controller.rb:27
   (4.6ms)  commit transaction
   app/controllers/users_controller.rb:27
  Rendering notification_mailer/complete_mail.text.erb within layouts/mailer
  Rendered notification_mailer/complete_mail.text.erb within layouts/mailer (0.3ms)
NotificationMailer#complete_mail: processed outbound mail in 30.1ms
Sent mail to basell.4rm.0823@gmail.com (1080.1ms)
Date: Wed, 07 Apr 2021 23:35:42 +0000
From: no-replay@gmail.com
To: basell.4rm.0823@gmail.com
Message-ID: <606e41ce550c_23fa109cc1074487@ip-172-31-63-72.ec2.internal.mail>
Subject: COMPLETE join your address
Mime-Version: 1.0
Content-Type: text/plain;
 charset=UTF-8
Content-Transfer-Encoding: 7bit

Dear Morooka ryosuke.

Thank you for registering!

Your page is ...http://localhost:3000/users/3

Redirected to https://e80e07be605e47f59609c70a46d6be94.vfs.cloud9.us-east-1.amazonaws.com/users/3
Completed 302 Found in 1129ms (ActiveRecord: 6.3ms)


下記は自身のアプリケーションに上記URLと全く同じ手順で導入し、sign_upした時のログ画面

上手くメール送信できていない

②自身のアプリケーション

Started POST "/users" for 123.221.22.117 at 2021-04-08 02:22:02 +0000
Cannot render console from 123.221.22.117! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by Devise::RegistrationsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"l8c8a2l0/mVBUnIA+27yLdEL7dx+xeFBC5kF4EYTFBrl5sRChtrbUqnQar7lwUIDRTBKNFxVn++qBBMq2mOjBg==", "user"=>{"name"=>"ryosue", "email"=>"basell@aaafa", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}
   (0.1ms)  begin transaction
   /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/activerecord-5.2.4.5/lib/active_record/log_subscriber.rb:98
  User Exists (0.2ms)  SELECT  1 AS one FROM "users" WHERE "users"."email" = ? LIMIT ?  [["email", "basell@aaafa"], ["LIMIT", 1]]
   /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/activerecord-5.2.4.5/lib/active_record/log_subscriber.rb:98
  User Exists (0.1ms)  SELECT  1 AS one FROM "users" WHERE "users"."name" = ? LIMIT ?  [["name", "ryosue"], ["LIMIT", 1]]
   /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/activerecord-5.2.4.5/lib/active_record/log_subscriber.rb:98
  User Create (2.5ms)  INSERT INTO "users" ("email", "encrypted_password", "name", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?)  [["email", "basell@aaafa"], ["encrypted_password", "$2a$12$ExOnya0aK9XpQ3Xg5CUFR.NsnlyzHiL/kGLGQX6/iKFq3vYmN/EY2"], ["name", "ryosue"], ["created_at", "2021-04-08 02:22:02.778259"], ["updated_at", "2021-04-08 02:22:02.778259"]]
   /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/activerecord-5.2.4.5/lib/active_record/log_subscriber.rb:98
   (8.3ms)  commit transaction
   /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/activerecord-5.2.4.5/lib/active_record/log_subscriber.rb:98
Redirected to https://1b9dfb0c7d21417692e16e921a9a4707.vfs.cloud9.us-east-1.amazonaws.com/users/6
Completed 302 Found in 342ms (ActiveRecord: 11.2ms)


まず2つを見比べて

Processing byのところが違う

①の方はUsersController#create as HTMLが実行されているのに対し

②の方はDevise::RegistrationsController#create as HTMLが実行されている

ここが原因でした

記事通り自身のアプリケーションにはUserControllerに書いても実行しないので

RegistrationsControllerにコードを書く必要があった


RegistrationsControllerを編集する手順

①rails g devise:controllers user

$ rails g devise:controllers users
Running via Spring preloader in process 46167
      create  app/controllers/users/confirmations_controller.rb
      create  app/controllers/users/passwords_controller.rb
      create  app/controllers/users/registrations_controller.rb
      create  app/controllers/users/sessions_controller.rb
      create  app/controllers/users/unlocks_controller.rb
      create  app/controllers/users/omniauth_callbacks_controller.rb


②ルーティングを変更する

 devise_for :users, :controllers => {
   :registrations => 'users/registrations',
   :sessions => 'users/sessions',
   :passwords => 'users/passwords'
  }


③RegistrationsControllerを変更する

def create
   @user = User.new(user_params)
    respond_to do |format|
    if @user.save
      NotificationMailer.complete_mail(@user).deliver_now 
      format.html { redirect_to @user, notice: 'User was successfully created.' }
      format.json { render :show, status: :created, location: @user }
    else
      format.html { render :new }
      format.json { render json: @user.errors, status: :unprocessable_entity }
    end
   end
  end
  
  private
    def user_params
     params.require(:user).permit(:name, :email, :password, :profile_image,:introduction)
    end



補足

パスワードはアプリパスワードを使用する











タイトルとURLをコピーしました