将Devise登录设置为根页面


82

我的路线使用了以下代码:

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

但是,当我注销并转到时/logout,出现以下错误:

没有路由匹配{:action =>“ new”,:controller =>“设备/会话”}

如何设置要执行的根路径:sign_in

Answers:


123

要跟踪那些询问该错误的人员,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

这对我来说很好


6
这给我发过一个无限循环,亲爱的读者谁有同样的问题,答案的下一步是这里stackoverflow.com/questions/19855866/...
Jngai1297

92
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上进行了测试。


1
我怀疑这全都取决于Rails和Devise的版本。这当然适用于带有Devise 3的Rails 4-其他大多数答案都没有。
克里斯·刘易斯

我认为这是最好的方法。它与Rails的4.2.7.1,并制定4.2.0
路易斯·莱斯卡诺Airaldi

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

我需要设置默认的主根。我觉得我昨晚整夜都在尝试(在发布问题之前),但现在可以了。如果您已注销,Devise会尝试将您重定向到我未定义的根路径。


14
任何想法我为什么要得到这个?找不到路径“ /”的设计映射。也许您忘了将路线包装在合并范围内?
baash05 2012年

有一个解决此“找不到设计映射”错误的解决方案,为了清楚起见,我在此页面的单独答案中发布了该信息:stackoverflow.com/a/12994856/400790
Peter Nixey,2012年

15

(这是作为建议的编辑发布的,但是应该是它自己的答案。我不知道它是否有意义。亲爱的匿名编辑:随时将这个答案重新发布为您自己的,并给我留下评论因此我将删除此副本。)

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

3
请注意,此路由是重定向。它不会在根URL上显示登录页面。为此,您需要创建路由devise_scope:user do root:to =>“ devise / sessions#new”结尾,如@PeterNixey所建议的那样
Robert

11

我把它与@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:在根路径声明中做广告。我也删除了,as: :authenticated_root因为我的应用程序中已经root_path在链接中引用了一些地方。通过省去该as: :authenticated_root部分,我不必更改任何现有链接。


3

我猜您有不同的用户角色。如果要这样做,则必须将这样的范围添加到用户资源:

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

您可以在此处阅读有关覆盖设计路线的更多信息:https : //github.com/plataformatec/devise/wiki/操作方法:-Change-the-default-sign_in-and-sign_out-routes


我无法访问注销路由的原因不是很多。当您注销时,注销路由会将您重定向到root用户。
Logan Bailey

1

其中一些解决方案过于复杂。只需使用Rails:

'get' 'users/root', to: 'users#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

0

使用rails 3.2devise 3.2.3,我设法将我的主页“ home#index ”(controller#action)设置为登录页面,进行以下更改。

#1将登录表单添加到主页:

<%= 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将方法resource_name,resource和devise_mapping添加到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创建了一个自定义会话控制器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
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.