Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Devise Login to be root page

I am using the following code for my routes:

devise_for :user, 
  :as => '', 
  :path_names => { 
    :sign_in => "", 
    :sign_out => "logout", 
    :sign_up => "register" 
  }

But when I'm logged out and I goto /logout I get the following error:

No route matches {:action=>"new", :controller=>"devise/sessions"}

How do I setup the root path to be to :sign_in action?

like image 812
Logan Bailey Avatar asked Feb 10 '11 08:02

Logan Bailey


3 Answers

To follow on from the people who are asking about the error Could not find devise mapping for path "/" there is a workaround.

You'll find that there is a clue in your logs which will probably say:

[Devise] Could not find devise mapping for path "/".
This may happen for two reasons:

1) You forgot to wrap your route inside the scope block. For example:

  devise_scope :user do
    match "/some/route" => "some_devise_controller"
  end

2) You are testing a Devise controller bypassing the router.
   If so, you can explicitly tell Devise which mapping to use:

   @request.env["devise.mapping"] = Devise.mappings[:user]

So I retried the approach but instead wrapping it (as @miccet suggets) inside a scope block:

devise_scope :user do
  root to: "devise/sessions#new"
end

This worked fine for me

like image 200
Peter Nixey Avatar answered Oct 15 '22 06:10

Peter Nixey


devise_for :users

devise_scope :user do
  authenticated :user do
    root 'home#index', as: :authenticated_root
  end

  unauthenticated do
    root 'devise/sessions#new', as: :unauthenticated_root
  end
end

Just like this, tested on Rails Rails 4.1.0.rc1.

like image 28
VvDPzZ Avatar answered Oct 15 '22 07:10

VvDPzZ


root :to => "devise/sessions#new"

I needed to set the default home root. I felt like I had tried this all night last night (prior to posting the question), but it's working now. If you're logged out, Devise attempts to redirect you to the root path which I had undefined.

like image 23
Logan Bailey Avatar answered Oct 15 '22 05:10

Logan Bailey