How to overriding devise named routes from refinerycms-authentication?

1,515 views
Skip to first unread message

Anatortoise

unread,
Mar 25, 2011, 8:26:36 PM3/25/11
to refine...@googlegroups.com
Hi,
  I'm trying to integrate refinerycms with an existing app that is already using devise, with existing user model and customized session controller.
MyApp routes take precedence (which is what I want) because they are earlier in the routing, but the named route helpers are overridden by whichever defined them last, or so it seems.  How can I add an initiailizer, or something, to let MyApp control the definition of the named route? For example:


>rake routes | grep "destroy_user_session"
destroy_user_session GET /users/sign_out(.:format)  {:action=>"destroy", :controller=>"users/sessions"} #from MyApp/config/routes.rb
destroy_user_session GET /registrations/logout(.:format)  {:action=>"destroy", :controller=>"sessions"} #from refinerycms-authentication/config/routes.rb

rails_console>include Rails.application.routes.url_for_helpers
=> Object
rails_console>destroy_user_session_path
=> "/registrations/logout" 

I was hoping for "/users/sign_out".

Seems like this would be a common problem with any rails engine defining routes, but I haven't found an answer online yet, so thought I'd ask directly.

Thanks for any help

Anatortoise

unread,
Mar 31, 2011, 7:53:50 PM3/31/11
to refine...@googlegroups.com
Answering my own question, in case someone else needs it. 

General problem: a gem adds routes, loaded after main app, that conflict with and override main app named routes. So recognize_path and url_helpers no longer agree.

Specific to refinerycms, if you are integrating with existing app that already uses devise, and need the main app devise routes rather than refinerycms devise routes...


A Solution:
Create MainApp/config/named_routes_overrides.rb containing..
Rails.application.routes.draw do
  #re-state the necessary routes here, in addition to stating them higher in routes.rb , sandwiching the conflicting gem route definitions above and below.
   devise_for :users, :controllers=>{:sessions=>"users/sessions"} do
       get "users/sessions/whatever" #example additional routes handled by custom session controller
   end
end

Add the following inside MainApp/config/application.rb

class Application < Rails::Application

   ...

   initializer 'add named route overrides' do |app|
      app.routes_reloader.paths << File.expand_path('../named_routes_overrides.rb',__FILE__)
      #this seems to cause these extra routes to be loaded last, so they will define named routes last.
    end
end

Test with rails console, before the fix, named routes don't match recognize path:

>Rails.application.routes.url_helpers.destroy_user_session_path
=> "/registrations/logout" #refinerycms named route
>Rails.application.routes.recognize_path("/users/sign_out")
=> {:controller=>"users/sessions", :action=>"destroy"}

After fix, they match:

>Rails.application.routes.url_helpers.destroy_user_session_path
=>"/users/sign_out"
Rails.application.routes.recognize_path("/users/sign_out")
=> {:controller=>"users/sessions", :action=>"destroy"}

Abraharl

unread,
May 8, 2012, 3:28:07 PM5/8/12
to refine...@googlegroups.com
This has been extremely helpful too me. However if your application wasn't using Devise or any other gems that effected the route.rb file how would you go about resolving the conflict between the routes added by the gem and the application routes?
Reply all
Reply to author
Forward
0 new messages