我的路线使用了以下代码:
devise_for :user,
:as => '',
:path_names => {
:sign_in => "",
:sign_out => "logout",
:sign_up => "register"
}
但是,当我注销并转到时/logout,出现以下错误:
没有路由匹配{:action =>“ new”,:controller =>“设备/会话”}
如何设置要执行的根路径:sign_in?
我的路线使用了以下代码:
devise_for :user,
:as => '',
:path_names => {
:sign_in => "",
:sign_out => "logout",
:sign_up => "register"
}
但是,当我注销并转到时/logout,出现以下错误:
没有路由匹配{:action =>“ new”,:controller =>“设备/会话”}
如何设置要执行的根路径:sign_in?
Answers:
要跟踪那些询问该错误的人员,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上进行了测试。
root :to => "devise/sessions#new"
我需要设置默认的主根。我觉得我昨晚整夜都在尝试(在发布问题之前),但现在可以了。如果您已注销,Devise会尝试将您重定向到我未定义的根路径。
我把它与@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部分,我不必更改任何现有链接。
我猜您有不同的用户角色。如果要这样做,则必须将这样的范围添加到用户资源:
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
其中一些解决方案过于复杂。只需使用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
使用rails 3.2和devise 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