简体   繁体   中英

Devise change password form not working

I'm trying to implement the password reset functionality in an app, using Devise. I'm at a point where the "forgot password" link works (in that it sends the email), and the email contains the link to "Change my password".

The change password link works and sends you to a form (at the URL http://localhost:3000/users/password/edit?reset_password_token=c7cKb4hMhSLRs72n9dYj ) asking for the new password (and to confirm it), but after typing in a new password and clicking "Chang my password", it redirects to localhost:3000/users/password and says "Please review the problems below:" and presents a from with one field for Email, and a button for "Send password reset instructions". This is, of course, the form for submitting your email to get password reset instructions, but it also has an error notice of "can't be blank", as if someone clicked the button with the field empty.

I used a tutorial to configure ActionMailer to send the email, which I thought was going to be the most difficult part of this process, but now I can't figure out why the form to change one's password is not working.

I'm very new to Rails, and this is my first time working with Devise and ActionMailer, so I apologize if I haven't included some needed information. Just let me know and I can add anything that helps.

Thanks.

Ok, so with the help of someone at a Rails meeting tonight, I found out what the problem was. It didn't really have anything to do with the controllers or routes, but rather I had caused the bug by incorrectly adding some code to the form. Here's the corrected code for my form:

<h2>Change your password</h2>

<%= simple_form_for(resource, :as => resource_name, :url => password_path(resource_name),
         html: { method: :put, class: "pure-form pure-form-stacked" }) do |f| %>
  <%= f.error_notification %>

  <%= f.hidden_field :reset_password_token %>

  <%= f.input :password, label: "New password", autofocus: true, required: true %>
  <%= f.input :password_confirmation, label: "Confirm new password", required: true %>

  <div><%= f.submit "Change my password", class: "pure-button" %></div>
<% end %>

<%= render "devise/shared/links" %>

My problem was that instead of including the class: "pure-form pure-form-stacked line as a second key/value pair into html: {} , I had a second html: {} line.

In case that's not clear, I had this:

html: { method: :put },
html: { class: "pure-form pure-form-stacked" }

instead of this:

html: { method: :put, class: "pure-form pure-form-stacked" }

and for some reason, in the first version, only the second html: {} was being submitted with the form, so it didn't get processed as a put request.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM