Custom Failure App in Engine

440 views
Skip to first unread message

Curtis Schiewek

unread,
Sep 29, 2014, 9:38:04 AM9/29/14
to plataforma...@googlegroups.com
Hello All,

So I have an engine, and inside said engine, I have a custom failure app that I want the engine to use.  I don't want to override the FailureApp in the devise initializer because I don't want to overwrite the Devise config of the main app.  I'm therefore specifying the failure_app in my devise_for call.  I'm requiring the auth_failure just after devise in my engine definition.  

The problem is I'm getting the following error when starting the app:

"failure_app.rb:13:in `<class:FailureApp>': undefined method `routes' for nil:NilClass (NoMethodError)"

Where should I be requiring my FailureApp so that it works?

routes.rb
  devise_for :users, failure_app: 'Api::AuthFailure', controllers: {
    sessions: 'api/devise/sessions',
    registrations: 'api/devise/registrations',
    passwords: 'api/devise/passwords',
    confirmations: 'api/devise/confirmations'
  }

api/auth_failure.rb
module Api
  class AuthFailure < Devise::FailureApp
    def redirect_url
      new_user_session_path
    end

    def respond
      if http_auth?
        http_auth
      else
        redirect
      end
    end
  end
end

api/engine.rb
require 'devise'
require 'api/auth_failure'

module Api
  class Engine < ::Rails::Engine
    isolate_namespace Api
  end
end


Thanks!

Lucas Mazza

unread,
Sep 29, 2014, 9:57:15 AM9/29/14
to plataforma...@googlegroups.com
This should be doable but I can't figure out what is wrong here. Can you reproduce this in a fresh app so anyone else can take a deeper look in the error stack and the engine code? Thanks!

--

---
You received this message because you are subscribed to the Google Groups "Devise" group.
To unsubscribe from this group and stop receiving emails from it, send an email to plataformatec-de...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Curtis Schiewek

unread,
Sep 29, 2014, 10:17:03 AM9/29/14
to plataforma...@googlegroups.com
I stepped through the code, and basically what's happening is Rails.application is nil when that class is being loaded.  I moved the require for the failure app to the routes file, which makes the previous error go away, but now I get 

"undefined local variable or method `new_user_session_path' for #<Api::AuthFailure:0x007fdae8f1b6c8>"

I'm guessing this is happening because I actually haven't defined that url yet.  I guess I can manually generate the new_user_session_path.  Do you see any other alternatives?

Curtis Schiewek

unread,
Sep 29, 2014, 11:10:11 AM9/29/14
to plataforma...@googlegroups.com
I got it working.  I left the require in my routes files and changed the redirect_url method in my failure app to this:

def redirect_url
  engine_route = Rails.application.routes.routes.select{|r| r.app == Api::Engine}.first
  "#{engine_route.path.spec.to_s}/users/sign_in"
end

I think that should be fine, as I'm not allowing the main app the rename the sign_in path, but am I missing something?  Is there a better way I'm not seeing?
Reply all
Reply to author
Forward
0 new messages