简体   繁体   English

Rails:如何确认在文本框中输入的电子邮件地址与当前用户的电子邮件地址匹配?

[英]Rails: How to confirm email address entered in text box matches current user's email address?

I am creating a page that allows a user to delete their account/profile. 我正在创建一个页面,该页面允许用户删除其帐户/配置文件。 On this page, I have a text box where the user types their email address to confirm they are indeed the current user and that they want to delete their account (upon button click). 在此页面上,我有一个文本框,用户在其中输入其电子邮件地址,以确认他们确实是当前用户,并且希望删除其帐户(单击按钮时)。

What I want to do is, when the user clicks the "Delete Account" button below the text box, I want to grab that text and compare it to the current user's email account. 我想做的是,当用户单击文本框下方的“删除帐户”按钮时,我要获取该文本并将其与当前用户的电子邮件帐户进行比较。 If it matches, I want to delete the user's account. 如果匹配,我要删除该用户的帐户。 If not, I want to flash an error message. 如果没有,我想刷新一条错误消息。

I have some of the logic figured out, and I will paste what I have below. 我已经弄清楚了一些逻辑,我将在下面粘贴我的内容。 Right now, if you click the 'Delete Account' button it deletes the user's account and redirects to the home page, and flashes a 'goodbye' message on the home page. 现在,如果您单击“删除帐户”按钮,它将删除用户的帐户并重定向到主页,并在主页上闪烁“再见”消息。 However, there is no comparison functionality b/t current user's email and text box entry. 但是,当前用户的电子邮件和文本框输入没有比较功能。 I'm not sure if I can do this in Rails or if I will have to incorporate JavaScript. 我不确定是否可以在Rails中做到这一点,或者是否必须合并JavaScript。 I am using Devise and Cancan. 我正在使用Devise和Cancan。

Please be as explicit as possible, I am fairly new to Rails. 请尽可能明确,我是Rails的新手。 Thank you! 谢谢!

Email box and button are at /settings/profile/delete, file is 'delete.html.haml'. 电子邮件框和按钮位于/ settings / profile / delete,文件为'delete.html.haml'。

The email box 邮箱

.field
  = label :email, 'Email Address'
  = email_field :email, 'Enter your email address', id: 'delete-email-box'

The Delete Account Button (below email box) 删除帐户按钮(在电子邮件框下方)

#confirm-delete-btn
  = link_to 'Delete Account', user_registration_path, method: :delete, data: 
   { confirm: 'Are you sure you want to delete your account? This cannot be 
   undone!' }, class: submit btn-delete'
  = link_to 'Cancel', profile_index_path, id: 'cancel-profile'

profile_controller.rb profile_controller.rb

def delete
  @user = current_user
  @profile = current_user.profile
end

def destroy
  @user = current_user
  @profile = current_user.profile
  if @profile.destroy
    @user.destroy
    redirect_to root_url, notice: "User deleted."
  else
    render_error_message
  end
end

Just looked at how to make a form to delete items and found this . 只是看着如何制作一个表格来删除项目并发现了这一点 So your form would look like this: 因此,您的表单将如下所示:

<% form_for(:user, :url => path_for_your_destroy_action(@user), :html => {:method => :delete}) do  %>
  email: <%= text_field_tag :mail %>
  <%= submit_tag "Delete" %>
<% end %>

Now inside your destory method you could simply check if email matches current users email and if it does that delete his account: 现在,在您的销毁方法中,您可以简单地检查电子邮件是否与当前用户的电子邮件匹配,并且如果这样做,则删除他的帐户:

def destroy
  if current_user.email == params[:email]
    current_user.destroy
    redirect_to your_path, notice: "user deleted"
  else
    render "your_form", notice: "invalid email"
  end
end

Adding on to David's answer, based off of your code it looks like you are also trying to delete the user's profile. 根据您的代码,添加到David的答案后,您似乎也在尝试删除用户的个人资料。 It isn't explicitly described how a user is related to a profile, but based off of how you're setting up things in your current destroy action I'm assuming its through some sort of association. 它没有明确描述用户与配置文件的关系,但是基于您如何在当前销毁操作中设置内容,我通过某种关联来假设用户。 In this case, you will still need to destroy the profile or it will remain as an unused row in your database. 在这种情况下,您仍然需要销毁概要文件,否则它将作为未使用的行保留在数据库中。 This can be accomplished through a simple addition to the destroy action: 这可以通过对destroy动作进行简单的添加来实现:

def destroy
  if current_user.email == params[:email]
    current_user.profile.destroy
    current_user.destroy
    redirect_to your_path, notice: "user deleted"
  else
    render "your_form", notice: "invalid email"
  end
end

However, this is clunky and can easily lead to errors in the future when you need to implement similar functionality elsewhere. 但是,这很笨拙,将来需要在其他地方实现类似功能时,很容易导致错误。 A much DRYer Rails way to deal with this in at the Model Layer: 在模型层使用多种DRYer Rails处理方式:

User.rb: User.rb:

has_one :profile, dependent: :destroy

This will ensure that anytime a user is destroyed, its associated profile is also automatically destroyed. 这将确保在销毁用户时,其关联的配置文件也将自动销毁。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在JavaScript中获取Liferay用户的电子邮件地址? - How to get Liferay user email address in JavaScript? 如何隐藏电子邮件地址? - How to hide an email address? 从电子邮件地址查找用户 - Find user from email address 如何使用 Bolt for JavaScript(Node.JS) 获取 Slack 机器人的当前用户 email 地址 - How to get the current user email address for Slack bot using Bolt for JavaScript(Node.JS) 当在该行中输入电子邮件地址时,如何让 Google 表格通过电子邮件发送该行? - How can I have Google Sheets email a row when an email address in entered into that row? 如何使用存储在数据库中的用户 email 地址将消息发送给特定用户? - How can I send the message to a specific user using the user's email address stored in db? 在流星发布功能中获取用户的电子邮件地址 - Getting user's email address in Meteor publish function Chrome 应用程序,获取登录用户的电子邮件地址 - Chrome App, getting logged in user's email address 一个简单的 jquery 片段来验证用户的 email 地址 - a simple jquery snippet to validate a user's email address Google云端硬盘和电子表格API:获取用户的电子邮件地址 - Google Drive & Spreadsheets API: Get the user's email address
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM