Rails and Devise - Send bad login feedback to client via Ajax
<p>I'm almost done modifying Devise to work with my custom login form, using AJAX. The only thing I have left to implement is getting messages about types of login failures (blank fields, incorrect credentials, etc.) to the custom form via AJAX when a user tries to log in incorrectly.</p>

<p>I have tried simply modifying the create method in sessions_controller.rb for Devise, like so:</p>

<pre><code>def create
  super
    respond_to do |format|
      format.js
    end
  end
end
</code></pre>

<p>And putting this code in create.js.erb for sessions views:</p>

<pre><code>$("#complete_error").text("&lt;%= "#{flash[:alert]}" %&gt;");
$("#incorrect-log").removeClass("hidden");
</code></pre>

<p>This is not working at all. Even writing <code>puts flash[:alert]</code> in the controller to test if any alert is generated doesn't yield any results. I've tried following other guides on AJAX logins, but they were a bit confusing and also tended not to address exactly what I was looking for.</p>

<p>What do I need to do to get any login alerts, should they exit, to be sent back to the client on an unsuccessful login attempt?</p>

<p><strong>EDIT:</strong></p>

<p>401 Unauthorized error in log:</p>

<pre><code>Started POST "/users/sign_in" for ::1 at 2016-08-18 22:38:42 -0400
Processing by SessionsController#create as JS
  Parameters: {"utf8"=&gt;"✓", "user"=&gt;{"login"=&gt;"aasdfasdf", "password"=&gt;"[FILTERED]"}, "commit"=&gt;"Log in"}
  User Load (0.3ms)  SELECT  `users`.* FROM `users` WHERE (lower(username) = 
  'aasdfasdf' OR lower(email) = 'aasdfasdf') ORDER BY `users`.`id` ASC LIMIT 1
Completed 401 Unauthorized in 2ms (ActiveRecord: 0.3ms)
</code></pre>

<p>Sessions Controller: (<em>Do I need to completely override the create method, not just super?</em>)</p>

<pre><code>class SessionsController &lt; Devise::SessionsController

  before_action :authenticate_user!, :only =&gt; [:control_panel]

  # GET /resource/sign_in
  def new
    super
  end

  # POST /resource/sign_in
  def create
    super
    respond_to do |format|
      format.js
    end
  end

  # DELETE /resource/sign_out
  def destroy
    super
  end

  def control_panel
    # Control panel page
  end

end
</code></pre>