检查模型是否可确认

Log*_*gar 2 ruby-on-rails devise

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

我在Rails应用程序中有两种基于设计的模型,一种是可以确认的,另一种不是。

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

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

  • 有没有一种比覆盖更可靠的方法after_sign_up_path来根据模型是否可确定来重定向到其他页面?

谢谢

sja*_*ime 5

您可以执行以下操作:

resource.class.devise_modules.include?(:confirmable)
Run Code Online (Sandbox Code Playgroud)

要么

resource.respond_to?(:confirmed?)
Run Code Online (Sandbox Code Playgroud)

关于检查用户是否已经确认,可以使用以下方法:

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
Run Code Online (Sandbox Code Playgroud)

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