My goal is to implement an Ajax login solution with Devise with the minimal change of patterns. I already reached it partially, but there is one problem with the failure callback. Let me explain the scenario:
- Display login form in a modal with
remote: true
Rails/UJS convention (OK); - Create a custom
sessions
controller and point Devise to it (OK); - Create a JS view file
create.js.erb
to respond forsessions#create
action (PROBLEM);
The problem: My create.js.erb
only contains an alert("Test ok")
. When I submit the sessions#new
form with the correct credentials the file create.js.erb
is executed, the alert
is shown. But with wrong credentials it doesn't, returning 401 Unauthorized
status and create.js.erb
is ignored.
Maybe someone know a quick trick to make create.js.erb
run when login fail. This way I don't need to create a standalone Ajax script or change entire sessions
controller.
Thank you,
The environment:
VERSIONS:
Rails 4.0.2
Devise 3.2.2
Custom sessions controller:
class Website::SessionsController < ::Devise::SessionsController
respond_to :js # without it neither on success create.js.erb runs
layout false # action `new` pure html which is rendered in a modal box
end
sessions/create.js.erb
alert("Test ok");
Server log when login fails:
Started POST "/users/sign_in" for 127.0.0.1 at 2014-03-27 09:59:47 -0300
Processing by Website::SessionsController#create as JS
Parameters: {"utf8"=>"✓", "user"=>{"email"=>"", "password"=>"[FILTERED]"}, "commit"=>"Fazer login"}
Completed 401 Unauthorized in 1ms