在Rails中确认电子邮件,而不使用任何现有的身份验证gem /插件

时间:2022-07-11 20:16:45

I'm working on this alerting service in Rails. And really, all I need to do is, when a user signs up, send a confirmation email to the user. And upon confirmation from the user, activate the user. I tried playing around with Matt Hooks' Authlogic email activation tutorial, but its really leading nowhere. So , any ideas how I can do this with minimum fuss ? Thanks !

我正在研究Rails中的这个警报服务。实际上,我需要做的就是,当用户注册时,向用户发送确认电子邮件。在用户确认后,激活用户。我试过玩Matt Hooks的Authlogic电子邮件激活教程,但它确实无处可去。那么,任何想法我怎么能做到这一点最小化?谢谢 !

UPDATE

UPDATE

So how i got devise to do the job for me is :

所以我如何设计为我做的工作是:

  1. Install the gem.

    安装宝石。

  2. Create a migration for devise's confirmable fields.

    为设计的可确认字段创建迁移。

  3. Specify

    指定

    devise :confirmable in your model.

    设计:在您的模型中确认。

  4. Create a confirm method in the relevant controller(and a route for that method) which would update the confirmed_at attribute of the relevant model.

    在相关控制器(以及该方法的路径)中创建确认方法,该方法将更新相关模型的confirmed_at属性。

  5. The devise generator creates a few views for you, one which is confirmation_instructions.html.erb. Customize the path there.
  6. 设计生成器为您创建了一些视图,其中一个是confirmation_instructions.html.erb。自定义那里的路径。

I used Rails 2.3.2 and I 've used this method along with Authlogic's authentication and it worked well. I do plan to switch to devise completely.

我使用了Rails 2.3.2并且我使用了这种方法以及Authlogic的身份验证,它运行良好。我打算完全切换到设计。

In all honesty, I wanted to accept both answers (unfortunately I can't do that), but its just that the devise solution seemed a easier solution.

老实说,我想接受这两个答案(遗憾的是我不能这样做),但它只是设计解决方案似乎更容易解决。

3 个解决方案

#1


70  

Assuming given the title that you definitely want to avoid Devise, Authlogic and friends, here's what I think you need to do:

假设你肯定想要避免使用Devise,Authlogic和朋友,这就是我认为你需要做的事情:

  • Create 'confirmation code' and 'confirmed' attributes in your user model.
  • 在您的用户模型中创建“确认代码”和“已确认”属性。
  • Create a new controller method on your user controller that expects a user id and confirmation code, looks up the user and then checks if the code in the parameter matches the code stored in the DB. If so, it clears the code and sets confirmed = true.
  • 在用户控制器上创建一个需要用户ID和确认代码的新控制器方法,查找用户,然后检查参数中的代码是否与存储在DB中的代码匹配。如果是这样,它会清除代码并设置confirm = true。
  • Create a route that maps e.g. /users/1/confirm/code to your new controller method.
  • 创建一个映射的路线,例如/ users / 1 /确认/代码到您的新控制器方法。
  • Create an ActionMailer template for the e-mail you want to send. This should accept a user as a parameter, and use the confirmation code of the user to send a mail containing a link to your new route.
  • 为要发送的电子邮件创建ActionMailer模板。这应该接受用户作为参数,并使用用户的确认代码发送包含新路由链接的邮件。
  • Create an observer for your user model. If the record is created or the e-mail address modified, then generate a random confirmation code, set it into the model and clear the confirmed flag. Then trigger your ActionMailer.
  • 为您的用户模型创建一个观察者。如果创建了记录或修改了电子邮件地址,则生成随机确认代码,将其设置到模型中并清除已确认的标志。然后触发你的ActionMailer。
  • Create a helper method which allows views to check if the current user is confirmed.
  • 创建一个帮助方法,允许视图检查当前用户是否已确认。
  • Use this method to enable/disable functionality as appropriate. Remember to protect your controller methods as appropriate as well as your view logic.
  • 使用此方法可以根据需要启用/禁用功能。请记住保护您的控制器方法以及视图逻辑。

#2


5  

You could also make use of scopes for selecting users.

您还可以使用范围来选择用户。

class User < ActiveRecord::Base
  scope :certified, where(:certified => true)
end

And then in your code:

然后在你的代码中:

@user = User.certified.find_by_username(foo)

#3


4  

Devise is an other excellent authentication gem that comes with email activation build in, perhaps you could give it a go.

Devise是另一个出色的身份验证宝石,内置了电子邮件激活,也许你可以试一试。

#1


70  

Assuming given the title that you definitely want to avoid Devise, Authlogic and friends, here's what I think you need to do:

假设你肯定想要避免使用Devise,Authlogic和朋友,这就是我认为你需要做的事情:

  • Create 'confirmation code' and 'confirmed' attributes in your user model.
  • 在您的用户模型中创建“确认代码”和“已确认”属性。
  • Create a new controller method on your user controller that expects a user id and confirmation code, looks up the user and then checks if the code in the parameter matches the code stored in the DB. If so, it clears the code and sets confirmed = true.
  • 在用户控制器上创建一个需要用户ID和确认代码的新控制器方法,查找用户,然后检查参数中的代码是否与存储在DB中的代码匹配。如果是这样,它会清除代码并设置confirm = true。
  • Create a route that maps e.g. /users/1/confirm/code to your new controller method.
  • 创建一个映射的路线,例如/ users / 1 /确认/代码到您的新控制器方法。
  • Create an ActionMailer template for the e-mail you want to send. This should accept a user as a parameter, and use the confirmation code of the user to send a mail containing a link to your new route.
  • 为要发送的电子邮件创建ActionMailer模板。这应该接受用户作为参数,并使用用户的确认代码发送包含新路由链接的邮件。
  • Create an observer for your user model. If the record is created or the e-mail address modified, then generate a random confirmation code, set it into the model and clear the confirmed flag. Then trigger your ActionMailer.
  • 为您的用户模型创建一个观察者。如果创建了记录或修改了电子邮件地址,则生成随机确认代码,将其设置到模型中并清除已确认的标志。然后触发你的ActionMailer。
  • Create a helper method which allows views to check if the current user is confirmed.
  • 创建一个帮助方法,允许视图检查当前用户是否已确认。
  • Use this method to enable/disable functionality as appropriate. Remember to protect your controller methods as appropriate as well as your view logic.
  • 使用此方法可以根据需要启用/禁用功能。请记住保护您的控制器方法以及视图逻辑。

#2


5  

You could also make use of scopes for selecting users.

您还可以使用范围来选择用户。

class User < ActiveRecord::Base
  scope :certified, where(:certified => true)
end

And then in your code:

然后在你的代码中:

@user = User.certified.find_by_username(foo)

#3


4  

Devise is an other excellent authentication gem that comes with email activation build in, perhaps you could give it a go.

Devise是另一个出色的身份验证宝石,内置了电子邮件激活,也许你可以试一试。