1
votes

I couldn't update user information following suggested procedures on devise's own page. When I click the Update button in the form it redirects back to to user/show without updating anything. The logs on console shows the RegistrationsController#update action was called instead the update_resource action from my custom RegistrationController.

logs:

Started PATCH "/users" for 127.0.0.1 at 2016-12-11 18:34:51 +0200 Processing by RegistrationsController#update as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"5vqLuQKyuJIoCx0mYfBUhIQmvS0Il5jy+ty1f2XDsehJM4i3M8mMPK5tdPpVQ6MFHIj05ZcRKO7rhlxroM75pQ==", "user"=>{"first_name"=>"Birhanu", "last_name"=>"Hailemariam2", "email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "current_password"=>"[FILTERED]"}, "commit"=>"Update User"} User Load (0.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]] Account Load (0.4ms) SELECT "public"."accounts".* FROM "public"."accounts" WHERE "public"."accounts"."subdomain" = $1 LIMIT 1 [["subdomain", "test3"]] User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]] (0.3ms) BEGIN (0.7ms) SELECT COUNT(*) FROM "users" (0.3ms) ROLLBACK Redirected to http://test3.lvh.me:3000/users/1 Completed 302 Found in 120ms (ActiveRecord: 4.5ms)

Started GET "/users/1" for 127.0.0.1 at 2016-12-11 18:34:51 +0200

views/devise/registrations/edit.slim

= simple_form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f|
          = f.input :first_name, autofocus: true 
          = f.input :last_name, autofocus: true 
          = f.input :email, autofocus: true 

          - if devise_mapping.confirmable? && resource.pending_reconfirmation? 
            div 
              |Currently waiting confirmation for: = resource.unconfirmed_email 

          .form-actions.col-sm-offset-3.m-b-xs    
            i 
              |(leave blank if you don't want to change it)
          = f.input :password, autocomplete: "off" 

          = f.input :password_confirmation, autocomplete: "off" 

          .form-actions.col-sm-offset-3.m-b-xs    
            i 
              |(we need your current password to confirm your changes)
          = f.input :current_password, autocomplete: "off" 

          .form-actions.form.col-sm-offset-3
            => link_to I18n.t('button.cancel'), :back, class: 'btn btn-primary btn-outline'
            = f.button :submit, class: 'btn btn-primary'

controller/registrations_controller.rb

class RegistrationsController < Devise::RegistrationsController

  protected    
  def update_resource(resource, user_params)
    resource.update_with_password(user_params)
  end

  def after_update_path_for(resource)
    current_account
  end

end

controllers/application_controller.rb

class ApplicationController < ActionController::Base
  .
  .
  before_filter :configure_permitted_parameters, if: :devise_controller?

  protected
  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:accept_invitation, keys: [:first_name, :last_name])
    devise_parameter_sanitizer.permit(:account_update, keys: [:first_name, :last_name, :email, :password, :password_confirmation, 
        :current_password])
  end
  .
  .
end

routes:

devise_for :users, controllers: { registrations: 'registrations' }
1
Did you update your routes to override devise registrations controller?Paweł Dąbrowski
Yes, I did config the route to the customer registration controller.bir_ham
is the problem that you are not giving it a new password?MZaragoza
@MZaragoza That shouldn't be a problem since I am the resource.update_with_password(user_params) and provided the current_password field which is only required to make the updates without providing the password and password_confirmation fields. As in devise page that's how it should be done. I've found the issue and gave an answer it in the answers section bellow.bir_ham

1 Answers

0
votes

I have managed to find what was causing the issue. It is because I have a private method that was returning false value to before_save ActiveRecord callback. Fact source: enter link description here

Previous version:

private
  def set_admin
    self.admin = User.count == 0 
  end

It should be changed to return nil in case of false value.

private
  def set_admin
    self.admin = User.count == 0 
    nil
  end

Now everything works, I can update user info!