Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails:使用Desive登录后重定向_Ruby On Rails_Devise - Fatal编程技术网

Ruby on rails Rails:使用Desive登录后重定向

Ruby on rails Rails:使用Desive登录后重定向,ruby-on-rails,devise,Ruby On Rails,Devise,使用Desive登录后,是否可以将用户重定向到不同的页面(基于角色)?它似乎只重定向到根:to=>。。。在routes.rb中定义的页面 谢谢 以下是我相信您在Desive wiki上寻找的答案: 默认情况下,designe在执行操作后会路由到root。在Desive Wiki上有一篇关于覆盖这些操作的好文章 或者,您可以将(资源)的存储位置设置为nil,然后对每个操作进行不同的重定向,即:注册后的路径(资源)、注册后的路径(资源)等等。您可以简单地将此方法添加到应用程序控制器: def aft

使用Desive登录后,是否可以将用户重定向到不同的页面(基于角色)?它似乎只重定向到根:to=>。。。在routes.rb中定义的页面


谢谢

以下是我相信您在Desive wiki上寻找的答案:


默认情况下,designe在执行操作后会路由到root。在Desive Wiki上有一篇关于覆盖这些操作的好文章


或者,您可以将(资源)的
存储位置设置为nil,然后对每个操作进行不同的重定向,即:注册后的
路径(资源)
、注册后的
路径(资源)
等等。

您可以简单地将此方法添加到应用程序控制器:

def after_sign_in_path_for(resource)
  user_path(current_user) # your path
end

您可以在应用程序控制器或需要执行操作的任何控制器中使用以下代码:

def after_sign_in_path_for(resource)
    users_path
end

designe在
的_path _中的_sign _之后有一个helper方法,可用于在登录/登录后覆盖默认的designe路由到root

要在登录后实现重定向到另一个路径,只需将此方法添加到应用程序控制器

#class ApplicationController < ActionController::Base

def after_sign_in_path_for(resource)
  users_path
end
在注册控制器-
app/controllers/admins/registrations\u controller.rb
文件中:

# app/controllers/admins/registrations_controller.rb

def after_sign_up_path_for(resource)
  dashboard_index_path
end
就这些

我希望这有帮助

我使用了示例1:

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception

  before_action :authenticate_user!

  protected

  def after_sign_in_path_for(resource)
    current_user.is_a?(Admin) ? admin_tests_path : (stored_location_for(resource) || root_path)
  end
end
class ApplicationController
谢谢,这就是我要找的。:-)没错。如果您查看Deave的注册控制器本身,您可以看到其中的所有不同方法,然后可以随意覆盖其中任何一种方法。@janders223您如何访问Deave的注册控制器?我运气不好:(该问题询问登录后的重定向,而此链接讨论如何更改登录和注销的路由(即创建和销毁会话)
# app/controllers/admins/registrations_controller.rb

def after_sign_up_path_for(resource)
  dashboard_index_path
end
class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception

  before_action :authenticate_user!

  protected

  def after_sign_in_path_for(resource)
    current_user.is_a?(Admin) ? admin_tests_path : (stored_location_for(resource) || root_path)
  end
end