Failures: 1) UserMailer reset_password_email ヘッダー情報・ボディ情報が正しいこと Failure/Error: @url = edit_password_reset_url(@user.reset_password_token) ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true # ./app/mailers/user_mailer.rb:11:in `reset_password_email' # ./spec/mailers/user_mailer_spec.rb:11:in `block (4 levels) in <main>' # ./spec/mailers/user_mailer_spec.rb:10:in `block (3 levels) in <main>' 2) パスワードリセット パスワードが変更できる Failure/Error: @url = edit_password_reset_url(@user.reset_password_token) ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true [Screenshot]: tmp/screenshots/failures_r_spec_example_groups_nested_6_パスワードが変更できる_900.png
これはtest.rbに以下のコマンドを追記
config.action_mailer.default_url_options = Settings.default_url_options.to_h end
あと、更新はできるけど 画面が変わらないという現象が起きた。 理由は、edit.html.erbでlocal trueがなかった。
password_resets/edit.html.erb
<% content_for(:title, t('.title')) %> <h1><%= t '.title' %></h1> <%= form_with model: @user, url: password_reset_path(@token),local: true, method: :put do |f| %> <%= f.label :email %> <%= @user.email %> <%= f.label :password %> <%= f.password_field :password, class: 'form-control' %> <%= f.label :password_confirmation %> <%= f.password_field :password_confirmation, class: 'form-control' %> <%= f.submit %> <% end %>
なぜlocal: true
が必要?
form_with
はデフォルトでAjax通信(非同期通信)になっており、それはつまり必要な箇所だけページが更新され、その他の箇所はそのままになるという事。だからページはリダイレクトされず、フラッシュメッセージも表示されなかったわけ。
local: true
と引数を渡す事で、これが通常のHTTPリクエストになり、ページ全体が返ってきてページがリロードされ、フラッシュメッセージも表示されるようになります!
あと、rubocopで以下のエラーが発生した。
Use the new Ruby 1.9 hash syntax.
ハッシュの書き方が原因らしい。
ex # 古いハッシュの書き方 :id => 1 # 新しいハッシュの書き方 id: 1
参考:
Notion – The all-in-one workspace for your notes, tasks, wikis, and databases.