Why does devise me redirect me to /users instead of /users/edit when my account update fails?

Vlad Otrocol

The problem is like this:

If I use the default registration controller I get redirected:

  • /users/edit to root when user update succeeds
  • /users/edit to /users when update fails

Because the default controller update action looks like this:

if resource_updated
  sign_in resource_name, resource, bypass: true
  respond_with resource, location: after_update_path_for(resource)
else
  clean_up_passwords resource
  respond_with resource
end

after_update_path_for is set by default to root

If update fails I get the devise error messages and hasError div wrappers for the fields with error

In my registrations controller in the edit action I attribute values to a variable @myvar={2, 3}

This variable is used in the registrations_edit view

Whenever user update fails I get error no method .each for nil for my @myvar

How should I declare my view variables such that they are accessible even when update form fails?!

I have tried:

Forcing the controller to redirect to edit view no matter what:

if resource_updated
  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

This created problems because devise validations do not work anymore

Edit-------

My registrations edit:

def edit
  @images = Dir.glob("public/assets/images/users/#{current_user.id}/med/*")
end

My view:

<% img_paths = Array.new %>
  <% @images.each do |image| %>
  <%img_paths.push([
    '',
    "/assets/images/users/#{current_user.id}/original/#{image.split('/').last}",
    {'data-img-src'=>"/assets/images/users/#{current_user.id}/med/#{image.split('/').last}"}
    ])%>
  <% end %>

Edit-----

Registration controller:

class Users::RegistrationsController < Devise::RegistrationsController

  def edit
    @images = Dir.glob("public/assets/images/users/#{current_user.id}/med/*")
  end

  def update
    if params[:image_file_path]
      ff = File.open("public/"+params[:image_file_path])
      resource.image = ff
      resource.save!
    end
    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_with_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
      respond_with resource, location: after_update_path_for(resource)
    else
      clean_up_passwords resource
      respond_with resource
    end
  end


  private

  def sign_up_params
    params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation, :telephone, :image, :address, :birthday)
  end

  def account_update_params
    params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation, :telephone, :image, :image_file_path, :address, :birthday)
  end

  protected 

  def update_resource(resource, params)
    resource.update_without_password(params)
  end

  def after_update_path_for(resource)
    edit_user_registration_path
  end

end
Arvind

You can try this;-

def update
    if params[:image_file_path]
      ff = File.open("public/"+params[:image_file_path])
      resource.image = ff
      resource.save!
    end
    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_with_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
      respond_with resource, location: after_update_path_for(resource)
    else
    @images = Dir.glob("public/assets/images/users/#{current_user.id}/med/*")

      clean_up_passwords resource

      respond_with resource
    end
  end

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Does the WSL from Windows 10 Spring update (2018) allows me to run docker on it? and why?

분류에서Dev

Why is my Makefile giving me problems?

분류에서Dev

Why is sudo telling me my password is wrong?

분류에서Dev

Why does my Xcode compiler tell me I use a value type even though I use classes?

분류에서Dev

Why does updating Oh My Zsh give me an error about rebasing with unstaged changes?

분류에서Dev

why does my float variable statement keeps on giving me an 'Cannot implicitly convert type double to float' error?

분류에서Dev

Why does feedly not load for me in Chrome Ubuntu?

분류에서Dev

Why does this provide me with a stack overflow error?

분류에서Dev

Why these expressions are giving me output instead of compile error?

분류에서Dev

Why is my tlistbox requiring me to double up my ampersands?

분류에서Dev

Angular: Why does my formArray not validate or update?

분류에서Dev

why does visual studio delete my classes when I update entity framework model

분류에서Dev

Why does my custom directive not update when I derive the output from the bounded data?

분류에서Dev

Can anyone tell me why my program crashes when I try to call the Insert or Delete function in a menu driven array manipulation program?

분류에서Dev

Don't understand why my code giving me an IndexError

분류에서Dev

Why does `ls -l` count more files than me?

분류에서Dev

Why does Export-Csv prompt me for an InputObject

분류에서Dev

Why does this 'kill' command log me out of the computer?

분류에서Dev

basecamp api tells me there's no basecamp account

분류에서Dev

Why does backbone filter collection fails when passing to view

분류에서Dev

why my case give me the errors?Why i can't put variables in switch cases?

분류에서Dev

Microsoft Graph API: Create events for users that are not me

분류에서Dev

Why Netbeans warns me of finalize?

분류에서Dev

Why my jdk can't work,and before the java_home, there is a space that is not from me

분류에서Dev

Why does powershell freeze for a bit when running my python scripts

분류에서Dev

Why does my code return wrong sum when i concatenate it?

분류에서Dev

Why does removing gwibber-service get rid of indicator-me

분류에서Dev

Why is microsoft using my account for RDP connections

분류에서Dev

Why does my Linux version name read Linaro instead of Xubuntu and how can I fix it?

Related 관련 기사

  1. 1

    Does the WSL from Windows 10 Spring update (2018) allows me to run docker on it? and why?

  2. 2

    Why is my Makefile giving me problems?

  3. 3

    Why is sudo telling me my password is wrong?

  4. 4

    Why does my Xcode compiler tell me I use a value type even though I use classes?

  5. 5

    Why does updating Oh My Zsh give me an error about rebasing with unstaged changes?

  6. 6

    why does my float variable statement keeps on giving me an 'Cannot implicitly convert type double to float' error?

  7. 7

    Why does feedly not load for me in Chrome Ubuntu?

  8. 8

    Why does this provide me with a stack overflow error?

  9. 9

    Why these expressions are giving me output instead of compile error?

  10. 10

    Why is my tlistbox requiring me to double up my ampersands?

  11. 11

    Angular: Why does my formArray not validate or update?

  12. 12

    why does visual studio delete my classes when I update entity framework model

  13. 13

    Why does my custom directive not update when I derive the output from the bounded data?

  14. 14

    Can anyone tell me why my program crashes when I try to call the Insert or Delete function in a menu driven array manipulation program?

  15. 15

    Don't understand why my code giving me an IndexError

  16. 16

    Why does `ls -l` count more files than me?

  17. 17

    Why does Export-Csv prompt me for an InputObject

  18. 18

    Why does this 'kill' command log me out of the computer?

  19. 19

    basecamp api tells me there's no basecamp account

  20. 20

    Why does backbone filter collection fails when passing to view

  21. 21

    why my case give me the errors?Why i can't put variables in switch cases?

  22. 22

    Microsoft Graph API: Create events for users that are not me

  23. 23

    Why Netbeans warns me of finalize?

  24. 24

    Why my jdk can't work,and before the java_home, there is a space that is not from me

  25. 25

    Why does powershell freeze for a bit when running my python scripts

  26. 26

    Why does my code return wrong sum when i concatenate it?

  27. 27

    Why does removing gwibber-service get rid of indicator-me

  28. 28

    Why is microsoft using my account for RDP connections

  29. 29

    Why does my Linux version name read Linaro instead of Xubuntu and how can I fix it?

뜨겁다태그

보관