Ruby-on-rails 将设计登录设置为根页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4954876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting Devise Login to be root page
提问by Logan Bailey
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 /logoutI get the following error:
但是当我注销并转到时,/logout我收到以下错误:
No route matches {:action=>"new", :controller=>"devise/sessions"}
没有路由匹配 {:action=>"new", :controller=>"devise/sessions"}
How do I setup the root path to be to :sign_inaction?
如何设置要执行的根路径:sign_in?
回答by Peter Nixey
To follow on from the people who are asking about the error Could not find devise mapping for path "/"there is a workaround.
从询问错误的人Could not find devise mapping for path "/"那里得到解决方法。
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:
所以我重试了这个方法,而是将它(如@miccet suggets)包装在一个范围块中:
devise_scope :user do
root to: "devise/sessions#new"
end
This worked fine for me
这对我来说很好用
回答by VvDPzZ
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.
就像这样,在 Rails Rails 4.1.0.rc1 上测试。
回答by Logan Bailey
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.
我需要设置默认的主根。我觉得我昨晚(在发布问题之前)整晚都尝试过这个,但现在它正在起作用。如果您已注销,Devise 会尝试将您重定向到我未定义的根路径。
回答by Gilles 'SO- stop being evil'
(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")
回答by Doug Steinberg
I got this to work with @VvDPzZ answer. But I had to modify it slightly
我得到了这个与@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
I had to ad to:in the root path declaration. I also removed the as: :authenticated_rootbecause I already had some places in my application referencing root_pathin links. By leaving out the as: :authenticated_rootpart I didn't have to change any of my existing links.
我不得不to:在根路径声明中添加广告。我还删除了 ,as: :authenticated_root因为我的应用程序中已经有一些地方root_path在链接中引用。通过省略该as: :authenticated_root部分,我不必更改任何现有链接。
回答by miccet
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
您可以在此处阅读有关覆盖设计路线的更多信息:https: //github.com/plataformatec/devise/wiki/How-To:-Change-the-default-sign_in-and-sign_out-routes
回答by Jesse Farmer
Some of these solutions are way too complex. Just use Rails:
其中一些解决方案太复杂了。只需使用 Rails:
Add 'get' 'users/root', to: 'users#root'to config/routes.rb.
添加'get' 'users/root', to: 'users#root'到 config/routes.rb。
In UsersController do something like:
在 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
回答by Rui Castro
Using rails 3.2and devise 3.2.3I manage to setup my home page "home#index" (controller#action) as the login page making the following changes.
使用rails 3.2和devise 3.2.3我设法将我的主页“ home#index”(controller#action)设置为登录页面,并进行了以下更改。
#1Added the login form to the home page:
#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 %>
#2Added methods resource_name, resource and devise_mapping to app/heldpers/application_helper.rb:
#2向app/heldpers/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
#3Created a custom sessions controller app/controllers/users/sessions_controller.rb:
#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
#4Skip the session routes and setup the custom sessions controller in config/routes.rb:
#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

