将“设计登录”设置为根页

我在我的路线中使用以下代码:

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

但是当我退出登录到 /logout时,我会得到以下错误:

没有路由匹配{ : action = > “ new”, 翻译: 控制器 = > “ design/session”}

如何设置根路径为 :sign_in操作?

62562 次浏览

I guess you have different user roles. If you do you have to add a scope like this to the users resource:

  devise_scope :user do
get "/logout" => "devise/sessions#destroy"
end

You can read more about overriding devise routes here: https://github.com/plataformatec/devise/wiki/How-To:-Change-the-default-sign_in-and-sign_out-routes

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.

(This was posted as a suggested edit, but should have been an answer of its own. I don't know if it makes sense or not. Dear anonymous editor: feel free to repost this answer as your own, and leave me a comment so I'll delete this copy.)

root :to => redirect("/users/login")

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

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.

Using rails 3.2 and devise 3.2.3 I manage to setup my home page "home#index" (controller#action) as the login page making the following changes.

#1 Added the login form to the home page:

<%= simple_form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
<%= f.input :email %>
<%= f.input :password %>
<%= f.button :submit %>
<% end %>

#2 Added methods resource_name, resource and devise_mapping to app/heldpers/application_helper.rb:

def resource_name
:user
end


def resource
@resource ||= User.new
end


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

#3 Created a custom sessions controller app/controllers/users/sessions_controller.rb:

class Users::SessionsController < Devise::SessionsController


protected


# This method tell sessions#create method to redirect to home#index when login fails.
def auth_options
{ scope: resource_name, recall: 'home#index' }
end


end

#4 Skip the session routes and setup the custom sessions controller in config/routes.rb:

devise_for :users, path: 'auth', skip: [:sessions],
controllers: {
sessions: 'users/sessions'
}


as :user do
get 'auth/sign_in' => 'home#index', as: :new_user_session
post 'auth/sign_in' => 'users/sessions#create', as: :user_session
delete 'auth/sign_out' => 'users/sessions#destroy', as: :destroy_user_session
end

I got this to work with @VvDPzZ answer. But I had to modify it slightly

  devise_scope :business_owner do
authenticated  do
root to: 'pages#dashboard'
end


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

I had to ad to: in the root path declaration. I also removed the as: :authenticated_root because I already had some places in my application referencing root_path in links. By leaving out the as: :authenticated_root part I didn't have to change any of my existing links.

Some of these solutions are way too complex. Just use Rails:

Add 'get' 'users/root', to: 'users#root' to config/routes.rb.

In UsersController do something like:

def root
if user_signed_in?
redirect_to root_for_signed_in_user_path (or whatever)
else
redirect_to new_user_session_path
end
end

I'm new on rails and I didn't know your 'device_scope' name have to be different to your 'device_for' name. Notice my example.

I tried this a hundred times a this is why it didn't work jajaja

  devise_for :user_devises, path: 'user_devises'


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

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