Trouble setting the app's root_url to an url setup through devise

103 views
Skip to first unread message

Asa

unread,
Nov 24, 2009, 10:28:12 AM11/24/09
to Devise
I'm having trouble when I try to setup the root url to be the login
page. Since there's a named route - new_user_session, I would think I
could setup my root like so: map.root :new_user_session. When I try
that I get the following backtrace, any ideas?

NoMethodError in SessionsController#new

You have a nil object when you didn't expect it!
The error occurred while evaluating nil.to_sym

devise-0.6.1/lib/devise/mapping.rb:32:in `find_by_path'
devise-0.6.1/lib/devise/mapping.rb:30:in `each_value'
devise-0.6.1/lib/devise/mapping.rb:30:in `find_by_path'
devise-0.6.1/lib/devise/controllers/helpers.rb:38:in `devise_mapping'
devise-0.6.1/lib/devise/controllers/helpers.rb:50:in
`is_devise_resource?'

Thanks for your help!

José Valim

unread,
Nov 24, 2009, 10:50:28 AM11/24/09
to plataforma...@googlegroups.com
It won't work because Devise extracts the scope to be logged in from the url path. So "/" won't work, because it expects "/users/something". This logic is contained in find_by_path, so an easy fix would do something like this in an initializer:

class Devise::Mapping
  class << self
    alias_method(:orig_find_by_path, :find_by_path)

    def find_by_path(path)
      if path == "/"
        Devise.mappings[:user]
      else
        orig_find_by_path(path)
      end
    end
  end
end
 
Or even changing in SessionsController:

class SessionsController
  prepend_before_filter :set_mapping_from_root

  protected
  def set_mapping_from_root
    @devise_mapping = Devise.mappings[:user] if request.path == "/"
  end
end
--
José Valim

Director of Engineering - Plataforma Tecnologia
http://blog.plataformatec.com.br/

trung

unread,
Nov 24, 2009, 11:07:57 AM11/24/09
to Devise
This is very simple.

Add this to your controller.

protected
def devise_mapping
@devise_mapping ||= Devise.mappings[:user]
end

trung

unread,
Nov 24, 2009, 11:11:09 AM11/24/09
to Devise
also
def is_devise_resource?
true
end

lancecarlson

unread,
Dec 30, 2009, 8:08:17 AM12/30/09
to Devise
Adding Trung's suggestions worked for me! Thanks!
Reply all
Reply to author
Forward
0 new messages