设置设置login为根页面

我正在使用下面的代码为我的路线:

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

但是,当我注销,我goto /logout我得到以下错误:

没有路线匹配{:action =>“new”,:controller =>“devise / sessions”}

如何设置根path:sign_in action?

从询问有关错误的人中继续Could not find devise mapping for path "/"有一个解决方法。

你会发现你的日志中有一个线索可能会说:

 [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] 

所以我重试了这个方法,而是在范围块中包装它(作为@miccet suggets):

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

这对我来说工作得很好

 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 

就像这样,在Rails Rails 4.1.0.rc1上testing。

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

我需要设置默认的主根。 我觉得我昨天晚上整个晚上都试过了(在发布问题之前),但现在正在工作。 如果你注销了,Devise试图将你redirect到我没有定义的根path。

(这是作为一个build议编辑发布,但应该是它自己的答案,我不知道是否有意义,亲爱的匿名编辑:随时重新发布这个答案作为你自己的,留下我的评论所以我会删除这个副本。)

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

我得到这个与@VvDPzZ答案。 但是我不得不稍微修改一下

  devise_scope :business_owner do authenticated do root to: 'pages#dashboard' end unauthenticated do root to: 'devise/sessions#new', as: 'unauthenticated_root' end end 

我不得不广告to:在根path声明中。 我也删除了as: :authenticated_root因为我的应用程序中已经有一些地方在链接中引用root_path 。 通过忽略as: :authenticated_root部分,我不必更改任何现有的链接。

我猜你有不同的用户angular色。 如果您需要将这样的范围添加到用户资源:

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

你可以阅读更多关于重写devise路线在这里: https : //github.com/plataformatec/devise/wiki/How-To :-Change-the-default-sign_in-and-sign_out-routes

使用rails 3.2devise3.2.3我设法设置我的主页“ home#索引 ”(控制器#动作)作为login页面进行以下更改。

#1添加login表单到主页:

 <%= 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应用程序/持有者/ application_helper.rb添加了方法resource_name,resource和devise_mapping:

 def resource_name :user end def resource @resource ||= User.new end def devise_mapping @devise_mapping ||= Devise.mappings[:user] end 

#3创build了自定义会话控制器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跳过会话路由并在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 

其中一些解决scheme太复杂。 只要使用你的Rails技能就可以把头部devise得更好:

'get' 'users/root', to: 'users#root' get''users 'get' 'users/root', to: 'users#root'添加'get' 'users/root', to: 'users#root' root'config / routes.rb。

在UsersController中执行如下操作:

 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