ログイン失敗時のメッセージをインスタンス変数とし、Viewで表示したいのですができません。
・controller( login_controller.rb )
class LoginController < ApplicationController def index @msg = 'これからログインします。' end def auth usr = User.find_by(username: params[:username]) if usr && usr.authenticate(params[:password]) then reset_session session[:usr] = usr.id redirect_to params[:referer] else flash.now[:referer] = params[:referer] @error = 'ユーザ名/パスワードが間違っています。' logger.debug('@errorをセットしました。') render action: :index end end end
・view( index.html.erb )
<p style="color: Red"> <%= @error %> <%= @msg %> </p> <%= form_with url: "/login/auth" do |form| %> <div class="field"> <%= form.label :username, 'ユーザ名' %> : <br /> <%= form.text_field :username, size: 20 %> </div> <div class="field"> <%= form.label :password, 'パスワード' %><br /> <%= form.password_field :password, size: 20 %> </div> <!--ログイン後にリダイレクトすべきアクションを隠しフィールドにセット--> <%= form.hidden_field :referer, value: flash[:referer] %> <%= form.submit 'ログイン' %> <% end %>
■事象
ログイン失敗ケース(authアクション、else内の処理)で@errorとして定義するメッセージが、render先で表示されない。
■調査状況
indexアクションで定義する@msgは正しくViewで表示される。
またlogからelse内に入っていることがわかるが、画面が変わらない。
Started POST "/login/auth" for 127.0.0.1 at 2022-03-13 08:51:24 +0900 Processing by LoginController#auth as TURBO_STREAM Parameters: {"authenticity_token"=>"[FILTERED]", "username"=>"aaaaa", "password"=>"[FILTERED]", "referer"=>"", "commit"=>"ログイン"} User Load (3.5ms) SELECT "users".* FROM "users" WHERE "users"."username" = ? LIMIT ? [["username", "aaaaa"], ["LIMIT", 1]] ↳ app/controllers/login_controller.rb:7:in `auth' @errorをセットしました。 Rendering layout layouts/application.html.erb Rendering login/index.html.erb within layouts/application Rendered login/index.html.erb within layouts/application (Duration: 28.9ms | Allocations: 171047) Rendered layout layouts/application.html.erb (Duration: 140.9ms | Allocations: 760213) Completed 200 OK in 210ms (Views: 157.6ms | ActiveRecord: 3.5ms | Allocations: 1165215)
分かる方、ご教示いただけると幸いです。
よろしくお願いいたします。