Search code examples
ruby-on-railsrubyauthenticationalertflash-message

Flash.alert not working in Ruby on Rails


I'm trying to have an alert pop-up when the user fails to login with the incorrect credentials. The login won't go through but the alert doesn't show up.

This is the code that I have in my view:

<h2>The Maintenance Functions are restricted to authorized users only.  Please login below.</h2>
<%= form_tag(dirlogin_pageout_path, :controller => "dirlogin", :action => "pageout", :method => "post") %>
<p>
    <label for="name">User ID:</label><br>
    <%= text_field_tag :userid, params[:userid] %>
</p>

<p>
    <label for="password">Password:</label><br>
    <%= password_field_tag :password, params[:password] %>
</p>

<%= submit_tag "login" %>

Then in my controller I have:

class DirloginController < ApplicationController
  def pagein
  end

  def pageout
  user = User.find_by_userid(params[:userid])
  if user and user.authenticate(params[:password])
      redirect_to maintenance_maintenancenav_path
  else
    render 'pagein'
    flash.alert = "Invalid name/password combination"
  end
end
end

I've also tried doing flash[:alert] = "Invalid name/password combination" but that doesn't work as well. Any help is appreciated.


Solution

  • You can use flash[:notice] instead.

    flash[:notice] = "Invalid name/password combination"
    

    And in your view you can display it with:

    <%= flash[:notice] %>