My understanding about routing is close to nil.
I have changed my edit registration page such that it should only change password.
In the original basic devise implementation I have 3 fields for password:
new password
new password confirmation
current password
By using update_without_password instead of update_with_password in my registrations controller I was able to get rid of the current_password field and allow other user attributes to be updated without current password confirmation
Another thing I changed was:
respond_with resource, location: after_update_path_for(resource)
else
clean_up_passwords resource
respond_with resource
to:
redirect_to edit_user_registration_path
else
clean_up_passwords resource
redirect_to edit_user_registration_path
I did this because in my edit action I had a @variable which was called in my edit view.
For some reason if a form fails instead of being redirected to /users/edit I was redirected to /users and got an error because of @variable was nil
After making these changes I don't get any password validation, no devise error messages and I can't update the password at all.
The view looks like this:
<div class="col-md-8 col-md-offset-2 well">
<h2 class="form-signin-heading text-center">Edit Profile</h2>
<%= devise_error_messages! %>
<%= render 'layouts/error' %>
<hr>
<div class="row">
<div class="col-md-4">
<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: {method: :put }) do |f| %>
<div class="form-group">
<%= f.label :password, "Change Password", class: 'control-label' %>
<i>(leave blank if you don't want to change it)</i><br />
<%= f.password_field :password, autocomplete: "off", class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :password_confirmation, "New Password Confirmation", class: 'control-label' %><br />
<%= f.password_field :password_confirmation, autocomplete: "off", class: 'form-control' %>
</div>
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<%= f.submit "Save", class: 'btn btn-success' %>
</div>
<%end%>
</div>
<hr>
</div>
</div>
My new Registration controller looks like this:
class Users::RegistrationsController < Devise::RegistrationsController
def update
self.resource = resource_class.to_adapter.get!(send(:"current_#{resource_name}").to_key)
prev_unconfirmed_email = resource.unconfirmed_email if resource.respond_to?(:unconfirmed_email)
resource_updated = resource.update_without_password(account_update_params)
yield resource if block_given?
if resource_updated
if is_flashing_format?
flash_key = update_needs_confirmation?(resource, prev_unconfirmed_email) ?
:update_needs_confirmation : :updated
set_flash_message :notice, flash_key
end
sign_in resource_name, resource, bypass: true
redirect_to edit_user_registration_path
else
clean_up_passwords resource
redirect_to edit_user_registration_path
end
end
private
def account_update_params
params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation, :telephone, :image, :image_file_path, :address, :birthday)
end
end
My log:
Started PUT "/users" for 127.0.0.1 at 2015-03-14 13:33:03 +0200
Processing by Users::RegistrationsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"a3WHTzlH8WHUsCqXVpxevnuwk2Ynf6EVXTYk2+8NnjI=", "user"=>{"password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Save"}
[1m[35mUser Load (1.0ms)[0m SELECT `users`.* FROM `users` WHERE `users`.`id` = 3 ORDER BY `users`.`id` ASC LIMIT 1
[1m[36mUser Load (1.0ms)[0m [1mSELECT `users`.* FROM `users` WHERE `users`.`id` = 3 LIMIT 1[0m
[1m[35m (1.0ms)[0m BEGIN
[1m[36m (1.0ms)[0m [1mCOMMIT[0m
Redirected to http://localhost:3000/users/edit
Completed 302 Found in 16ms (ActiveRecord: 4.0ms)
Aucun commentaire:
Enregistrer un commentaire