Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/56.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 设计:多用户类型_Ruby On Rails_Devise - Fatal编程技术网

Ruby on rails 设计:多用户类型

Ruby on rails 设计:多用户类型,ruby-on-rails,devise,Ruby On Rails,Devise,为了在Desive中添加安全性,我需要设置“before_filter”选项,如: before_filter :authenticate_student!, only: [:new, :edit] 这很好。。。但我的应用程序需要两种用户类型。。。学生和老师。我如何让控制器只检查其中任何一个是否为身份验证 比如: 我怎样才能存档 我使用的是Ruby 2.2.0和rails 4。只需在应用程序控制器中定义这些方法即可 class ApplicationController < Action

为了在Desive中添加安全性,我需要设置“before_filter”选项,如:

before_filter :authenticate_student!, only: [:new, :edit]
这很好。。。但我的应用程序需要两种用户类型。。。学生和老师。我如何让控制器只检查其中任何一个是否为身份验证

比如:

我怎样才能存档


我使用的是Ruby 2.2.0和rails 4。

只需在应用程序控制器中定义这些方法即可

class ApplicationController < ActionController::Base
  def authenticate_student!
     authenticate_user! && current_user.student?
  end

  def authenticate_any!
    authenticate_user!
  end
end
class ApplicationController

您可以完成如何检查学生的代码。

根据您的描述,听起来用户可能有不同的“角色”。其中用户可以是学生或教师(或两者都是?)。如果是这样,此功能的另一种方法是根据与课程的关系设置角色,并且只有一种类型的用户。然后你可以基于角色而不是身份验证进行限制。看看这个答案:似乎正是你想要的。我的学生/教师表真的不同。教师将扮演不同的角色,需要不同的属性。这就是为什么我当时和学生们分开了。
class ApplicationController < ActionController::Base
  def authenticate_student!
     authenticate_user! && current_user.student?
  end

  def authenticate_any!
    authenticate_user!
  end
end