javascript - Rails: How to confirm email address entered in text box matches current user's email address? -
i creating page allows user delete account/profile. on page, have text box user types email address confirm indeed current user , want delete business relationship (upon button click).
what want is, when user clicks "delete account" button below text box, want grab text , compare current user's email account. if matches, want delete user's account. if not, want flash error message.
i have of logic figured out, , paste have below. right now, if click 'delete account' button deletes user's business relationship , redirects home page, , flashes 'goodbye' message on home page. however, there no comparing functionality b/t current user's email , text box entry. i'm not sure if can in rails or if have incorporate javascript. using devise , cancan.
please explicit possible, new rails. thank you!
email box , button @ /settings/profile/delete, file 'delete.html.haml'.
the email box
.field = label :email, 'email address' = email_field :email, 'enter email address', id: 'delete-email-box'
the delete business relationship button (below email box)
#confirm-delete-btn = link_to 'delete account', user_registration_path, method: :delete, data: { confirm: 'are sure want delete account? cannot undone!' }, class: submit btn-delete' = link_to 'cancel', profile_index_path, id: 'cancel-profile'
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 @ how create form delete items , found this. form this:
<% form_for(:user, :url => path_for_your_destroy_action(@user), :html => {:method => :delete}) %> email: <%= text_field_tag :mail %> <%= submit_tag "delete" %> <% end %>
now within destory method check if email matches current users email , if delete 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
javascript ruby-on-rails ruby devise cancan
No comments:
Post a Comment