Cameron Bass Cameron Bass - 1 month ago 10
Ruby Question

Devise Session Controller. Custom routing on failure

Right now I'm extending the Devise sessions controller. Everything is working fine except that when the password is typed wrong or it can't find the user. it tries to redirect_to the sessions#new. I do not want that for my case I want it to redirect to a custom route because this is coming from a different view than the sessions new view. I know about

after_sign_in_path_for
but I'm not sure that is what I want because my particular case is the warden.authenticate! method. In the auth_options there is a recall hash. That is where I want to customize my route. Here is my sessions controller.

Session Controller



class SessionsController < Devise::SessionsController
def create

super do |user|
if params[:user][:invitation_id].present?
invitation = Invitation.find(params[:user][:invitation_id])

if !user.accounts.exists?(id: invitation.account.id)
user.accounts << invitation.account
flash[:tip_off] = "You now have access to your project \"#{invitation.account.name}\""
else
flash[:tip_off] = "You are already a member of #{invitation.account.name}"
end

cookies[:account_id] = invitation.account.id
invitation.destroy
user.save
end
end
end
end


As I stated before this works fine I just need a custom routes upon failure of password or email. Right now it goes to sessions#new, that does not work for my case. Any help would be great! Thanks

Answer

You can override recall value on auth_option method to redirect to another route on failure.

def auth_option
  { scope: resource_name, recall: "#{controller_path}#another_new_path" }
end
Comments