My issue seems to be quite complicated, but let's simplify it. I am working with devise and I need to display error messages below the form on my login page. Currently, when a user enters the wrong password, I see an error POST 401 (Unauthorized)
in the console, but the errors are not showing up on the page.
slice_code.js.erb
$(document).ready(function() {
var form = $('.centralized-login')
form.submit( function(event) {
event.preventDefault()
var email = $('#email-input').val()
var password = $('#password-input').val()
var req = $.ajax('/user_role', {
data: {
email
}
})
req.then( function(result) {
var { role } = result
switch (role) {
case 'company_manager':
$.ajax('/users/sign_in', {
method: "POST",
data: {
"user[email]": email ,
"user[password]": password,
"authenticity_token": $('input[name="authenticity_token"]').val()
},
success: function(data, textStatus, req){
window.location="/users/sign_in"
}
})
break;
Do you think I should add something like $("ajax:error")
?
_new.html.erb
<div class="col-sm-8 col-md-6 col-xs-12">
<div class="vr-textfield-wrapper">
<%= f.email_field :email, id: "email-input", class: "vr-textfield", placeholder: t('users.passwords.new.email_placeholder'), required: true, aria_required: true, autofocus: true %>
<span class="vr-field__invalid-msg"><%= t 'users.passwords.new.valid_email'%></span>
<% if resource.errors.full_messages_for(:email).first %>
<div class="vr-textfield-wrapper">
<div class="alert alert-danger">
<div class="error-explanation">
<%= t('activerecord.errors.models.user.attributes.email.not_found_in_database') %>
</div>
</div>
</div>
<% end %>
</div>
</div>
Edit
success: function(data, textStatus, req){
window.location="/users/sign_in"
},
error: function (jqXHR, textStatus, errorThrown) {
$('.centralized-login').find('.error-explanation').html(errorThrown)
console.log(textStatus + " " + errorThrown);
}
})
sessions_controller.rb
class Users::SessionsController < Devise::SessionsController
include LogoutUsergroups
def after_sign_out_path_for(_user)
new_user_session_path
end
end