Skip to main content
 首页 » 编程设计

ruby-on-rails之检查模型是否可确认

2025年12月25日17powertoolsteam

我对 Rails 还算陌生,所以如果我做错了,请告诉我:)

我在 Rails 应用程序中使用 devise 构建了两个模型,一个是可确认的,另一个则不可确认。

我想在用户主页或单独的页面上设置 after_sign_up_path_for(resource) ,指示已发送确认电子邮件,具体取决于我的资源是否必须确认其注册.

  • 我可以检查数据模型中是否存在确认字段,但是是否有更简洁的方法来执行此操作,例如 resource.confirmable?

  • 是否有比覆盖 after_sign_up_path 更简洁的方法来根据模型是否可确认重定向到不同页面?

谢谢

请您参考如下方法:

您可以执行以下操作:

resource.class.devise_modules.include?(:confirmable) 

或者

resource.respond_to?(:confirmed?) 

关于检查用户是否已确认,您可以使用以下内容:

def after_sign_up_path_for(resource) 
  if resource.class.devise_modules.include?(:confirmable)  
    if resource.active_for_authentication? 
      "path for confirmed user" 
    else 
      "path for waiting for confirmation" 
    end 
  else 
    "path for non confirmable model" 
  end 
end 

查看文档:http://www.rubydoc.info/github/plataformatec/devise/Devise/Models/Confirmable